Using ASP.Net Core 2 web application to invoke a complete library using EF6

I am trying to call the .Net 4.7 access library (which uses Entity Framework 6) from a new Asp Net Core 2.0 web application.

The problem is that EF6 doesn't seem to be able to get DbProviderFactory. My theory of work is that this is what should be provided in the app / web.config of the calling program. The error I get is:

System.TypeLoadException: 'Failed to load type' System.Data.Common.DbProviderFactories 'from the assembly' System.Data, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 '.'

To get around this problem, I created the DbConfiguration class:

public class MyDbConfiguration : DbConfiguration { public MyDbConfiguration() { SetProviderFactory("System.Data.SqlClient", System.Data.SqlClient.SqlClientFactory.Instance); SetProviderServices("System.Data.SqlClient", SqlProviderServices.Instance); } } [DbConfigurationType(typeof(MyDbConfiguration))] public class MyDbContext : DbContext, IMyDbContext { public MyDbContext(string connectionString) : base(connectionString) { } 

The breakpoint indicates that it correctly executes MyDbConfiguration, but still throws an error. I installed the System.Data.SqlClient and System.Data.Common packages in a .Net Core web application.

I did not find anything that explicitly says that what I am trying to do (in general) is impossible, so I am working on the fact that something is wrong with my DBConfiguration implementation. Please can someone point me in the right direction?

+7
asp.net-core entity-framework-6
source share
1 answer

If you want to use the library with Entity Framework from a .Net Core application, you must redirect the application to the .NET Framework. Here is a quote from an official source :

To use Entity Framework 6, your project must compile in .NET. Framework because Entity Framework 6 does not support .NET Core. if you need cross-platform features, you will need to upgrade to the Entity Framework Core .

If you check out a sample .Net.Core project ( linked from the same document) that uses the library with EF6 (just your case), you will see that it targets the .Net Framework, not the .Net Core:

 <TargetFramework>net452</TargetFramework> 

With this re-hook, you will not lose any of the .Net Core functionality that you are currently using. You can still use whatever we like in .Net Core. However, you are limiting the platform on which your application can only run on the .Net Framework. Unfortunately, you currently have no workaround for this limitation, since this is because the Entity Framework is only implemented for the .NET Framework. Your options will either shift towards the Entity Framework Core, or wait until the Entity Framework becomes part of the .Net standard.

In general, to fix your current problem, change the following line in the .Net Core csproj file:

 <TargetFramework>netcoreapp2.0</TargetFramework> 

to

 <TargetFramework>net47</TargetFramework> 
+3
source share

All Articles