*.net standard 2.0 accessing data from entity framework that’s in .net 4.7.2

iqworks Information Quality Works 276 Reputation points
2022-10-17T21:17:16.473+00:00

I have a blazor project. I want to call a method in .net standard (grabMbsaMember). This method calls a method in my framework 4.7.2 (GetMemberById). This method should return member data (customer) information back to .net standard, and then to my blazor project.
Here is my configuration :
This is from my blazor project :
251197-mbsa-core-access-to-net-framework.png

This is from my .net framework 4.7.2
251217-mbsa-core-access-to-net-framework1.png

This is my EF installed on my .net framework 4.2.7 .
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">

This is my .net standard 2.0

251203-mbsa-core-access-to-net-framework2.png

My blazor project calls this method in .net standard:
251218-mbsa-core-access-to-net-framework3.png

This is the method in .net standard: grabMbsaMember
251219-mbsa-core-access-to-net-framework4.png

This is the application config (appsettings.json in my blazor application). the error method
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
//20220718 RC@2021102008b added connection string
//20221013 - RC@2021102008g added MBSAnalysisData
"AllowedHosts": "*",
"ConnectionStrings": {
"MbsaDataEntities": "Data Source=IQWORKS120\MSSQLSERVER2012;Initial Catalog=MBSAnalysisData;User Id = eflogin;Password=eflogin;Integrated Security=True;",
"default": "Data Source=IQWORKS120\MSSQLSERVER2012;Initial Catalog= MBSAnalysisData ;User Id = eflogin;Password=eflogin;Integrated Security=True;"

}
}

This is the method in my .net 4.7.2 I am calling from .net standard. This should return a members information from the .net 4.7.2 entity framework 6.0.0.0.
251169-mbsa-core-access-to-net-framework5.png

This is the error I get …
blazor.server.js:1 [2022-10-17T18:54:39.169Z] Error: System.InvalidOperationException: No connection string named 'MbsaDataEntities' could be found in the application config file.
at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.Where[TSource](IQueryable1 source, Expression`1 predicate)
at Mbsa.BL.MembersService.GetMemberById(Int32 id) in D:\MBSSys\Mbsa\Mbsa 2021\BL\MembersService.cs:line 902
at MbsaDotNetStandard.MbsaUseFrameworkMethods.grabMbsaMember(Int32 mno) in D:\MBSSys\MbsaDotNetStandard\MbsaUseFrameworkMethods.cs:line 17
at MbsaBlazorServerSideEF.Pages.MbsaMemberPage.OnInitializedAsync() in D:\MBSSys\MbsaBlazorServerSideEF\Pages\MbsaMemberPage.razor:line 90
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,649 questions
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,496 Reputation points Microsoft Vendor
    2022-10-18T08:12:35.617+00:00

    @iqworks Information Quality Works , Welcome to Microsoft Q&A, based on my test, I didn't reproduce your problem. But I get the empty data after using your steps.

    After some researching, I could get the related data.

    First, Please change your tagetframework from .net standard 2.0 to .net standard 2.1.

    Second. Please add a App.Config to your .NET 6.0 Console app and add the connection string to the app.config.

    Like the following:

    <?xml version="1.0" encoding="utf-8" ?>  
    <configuration>  
      
      <connectionStrings>  
        <add name="MbsaDataEntities" connectionString="Data Source=IQWORKS120\\MSSQLSERVER2012;Initial Catalog=MBSAnalysisData;User Id = eflogin;Password=eflogin;Integrated Security=True;",  
    "default": "Data Source=IQWORKS120\\MSSQLSERVER2012;Initial Catalog= MBSAnalysisData ;User Id = eflogin;Password=eflogin;Integrated Security=True;" providerName="System.Data.SqlClient"/>  
      </connectionStrings>  
    </configuration>  
    

    Hope the above advice could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and 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.


  2. iqworks Information Quality Works 276 Reputation points
    2022-10-18T22:25:30.98+00:00

    Hi Bruce and Jack, thanks for getting back again.
    I looked at that multi-targeting where you can use both .net standard 2.0 and another project for 2.1. It looks like it would be great for a shop that has a huge .net 4.7.2 and your moving to core.
    But I also noticed that I could use it to access existing 4.7.2 methods that are already created that access the EF in the 4.7.2 environment. This could mean that I could use this multi-targeting to access ALL the EF methods in 4.7.2 that return EF data?
    I guess I have to make sure to document how I do this EXTRA well so I can read what I was trying to do :-).
    Thing is, I am thinking now :
    1 - i could just use . EF core and other core projects to access data and add new programs and projects for anything and everything I need for all future projects. Espcially since I can already access the same data base from core EF and .net 4.7.2.
    2 - I could use mutli-targeting so I can access all existing .net 4.7.2 EF methods that return 4.7.2 EF data.

    Thanks for both of your opinons, advice and suggestions

    0 comments No comments