Can i instantiate a repository pattern in .net 4.7.2 from .net standard 2.0

iqworks Information Quality Works 331 Reputation points
2022-10-01T21:00:40.067+00:00

I believe my problem is that from .net standard, i cannot instantiate an entity framework respository from .net 4.7.2?

It starts when my .net blazor server side calls .net standard method grabMbsaMember(1):
246547-instantiate-0.png
MbsaDotNetStandard.MbsaUseFrameworkMethods oou = new MbsaDotNetStandard.MbsaUseFrameworkMethods();
Mbsa.Models.MbsaMember mbrt = oou.grabMbsaMember(1);

And that method calls Mbsa.BL.MembersService() in .net 4.7.2
246674-instantiate-1.png
// core projects
using System;
using System.Collections.Generic;
using System.Text;
using Mbsa;

namespace MbsaDotNetStandard  
{  
    public class MbsaUseFrameworkMethods  
    {  
        public Mbsa.Models.MbsaMember ***grabMbsaMember***(int mno)  
        {  
            (method MembersService() in .net 4.7.2 where it bombs)  
            ***Mbsa.BL.MembersService mservice = new Mbsa.BL.MembersService();***  
            
            Mbsa.Models.MbsaMember mmember = mservice.GetMemberById(mno);  
  
            return mmember;  
              
        }  
         
    }  

246702-instantiate-2.png
In this 4.7.2, I am using a repository pattern.
public MembersService()
{
(this method calls the MbsaMember table in my data base and this is where it bombs)
_repository = new CommonRepo<MbsaMember>(); // General call
}
It seems like I cannot instantiate (new) the entity framework on my _repository var that holds a reference to all the tables in my database so I don’t have to instantiate each table when I need to use it??

Entity framework 6.0.9 is NOT compatable with .net 4.7.2.
Could this be the problem.

This is the error messasge:
[2022-09-21T20:03:02.104Z] Error: System.IO.FileNotFoundException: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
File name: 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
at Mbsa.Common.CommonRepo`1..ctor()
at Mbsa.BL.MembersService..ctor() in D:\MBSSys\Mbsa\Mbsa 2021\BL\MembersService.cs:line 38
at MbsaDotNetStandard.MbsaUseFrameworkMethods.grabMbsaMember(Int32 mno) in D:\MBSSys\MbsaDotNetStandard\MbsaUseFrameworkMethods.cs:line 14
at MbsaBlazorServerSideEF.Pages.MbsaMemberPage.OnInitializedAsync() in D:\MBSSys\MbsaBlazorServerSideEF\Pages\MbsaMemberPage.razor:line 91
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

I am not sure of where it needs this EntityFramework, Version=6.0.0.0

thanks for any advice or suggestions

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} vote

Accepted answer
  1. Anonymous
    2022-10-03T06:30:14.207+00:00

    It seems that you create and implement new netstandard2.0 class library (or service) that reference another service project built on .net framework v4.7.2, and now you'd like to perform EF database operations by consuming new methods in netstandard2.0 project from Blazor Server app.

    You can try to install EntityFramework in Blazor Server app, then build and run the project to check if it can work as expected.

    <PackageReference Include="EntityFramework" Version="6.4.4" />  
    

    Testing code below work well on my side, you can refer to it and compare with your projects.

    Call method in netstandard2.0 project from Blazor server app

    var ts = new MyStu();  
    var success = ts.create();  
    
    if (success)  
    {  
        //code logic here  
    

    Method in netstandard2.0 project

    public class MyStu  
    {  
        public bool create()  
        {  
            var ts = new TestService();  
            return ts.CreateStudent();  
        }  
    

    Method CreateStudent in .net framework v4.7.2 project

    public bool CreateStudent()  
    {  
        SchoolDbContext db = new SchoolDbContext("connection_string_here");  
                  
        db.Students.Add(new Student  
        {  
            FirstMidName = "F",  
            LastName = "L"  
        });  
      
        db.SaveChanges();  
      
        return true;  
    }  
    

    Test Result

    246904-capture2.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    With Regards,
    Fei Han


2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-10-03T15:21:19.727+00:00

    No, your blazor code can not call 4.7 code even via a .net standard wrapper. This is because the net core runtime which is hosting the loaded dll’s is not compatible.

    You will need to convert the 4.* code to .net standard or .net core

    Your real issue is EF6 is only supported by 4.* runtime, or netstandard 2.1. You can upgrade your 4.7 respository library to netstartard 2.1, but it will not be callable from 4.* or netstandard 2.0 projects.

    Using two project files and conditional compiles, you might be able build a 4.* project and and .netstandard 2.1 project. You also might be able to convert to a multi project msbuild file.


  2. iqworks Information Quality Works 331 Reputation points
    2022-10-03T17:06:27.653+00:00

    Also, I will check out ****"Using two project files and conditional compiles, you might be able build a 4.* project and and .netstandard 2.1 project. You also might be able to convert to a multi project msbuild file."**** It seems like it could get convoluted :-)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.