I'm trying a call a .Net 4.7 data access library (which uses Entity Framework 6) from a new Asp Net Core 2.0 Web Application.
The issue is that EF6 can't seem to get hold of a DbProviderFactory. My working theory is that this is something that should be provided in the app/web.config of the calling program. The error that I'me getting is:
System.TypeLoadException: 'Could not load type 'System.Data.Common.DbProviderFactories' from assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.'
In an effort to circumvent this issue, I created a 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)
{
}
A breakpoint shows that it executes the MyDbConfiguration correctly, but still throws the error. I've installed the System.Data.SqlClient and System.Data.Common packages on the .Net Core web app.
I haven't found anything that explicitly says what I'm trying to do (in general) is not possible, so I'm working on the assumption that there's something wrong with my DBConfiguration implementation. Please could someone point me in the right direction?
You can't put an EF6 context in an ASP.NET Core project because . NET Core projects don't support all of the functionality that EF6 commands such as Enable-Migrations require.
Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.
There is no edmx support in Entity Framework Core. It only supports a code-first approach. The option to add a new Entity Data Model will be added, but it will produce entity class files instead of an edmx file. The tooling development is a little bit behind the framework development currently.
Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology. EF Core can serve as an object-relational mapper (O/RM), which: Enables .NET developers to work with a database using .NET objects.
They are commonly called as Verbs, GET, PUT, POST, and DELETE are the most common ones. Consume (Call) Web API in ASP.NET Core So create a new empty project by using the ASP.NET Core Web Application (.NET Core) template, and name the project as APIConsume. Remember to select the framework as.NET Core and version as ASP.NET Core 5.0.
To reference ASP.NET Core, add the following <FrameworkReference> element to your project file: Referencing ASP.NET Core in this manner is only supported for projects targeting .NET Core 3.x. For information on using ASP.NET Core 5.0 and later APIs in a class library, see this GitHub issue.
ASP.NET Core Web Application is an Open-Source, Cloud-Optimized, High-Performance, Cross-Platform framework for building modern web apps which can be executable on Windows, Mac, and Linux. It includes the MVC framework to combine the features of Web API and MVC into a single web programming framework.
ASP.NET Core adheres to the .NET Core support policy. Consult the support policy when determining which ASP.NET Core versions to support in a library. A library should: Make an effort to support all ASP.NET Core versions classified as Long-Term Support (LTS). Not feel obligated to support ASP.NET Core versions classified as End of Life (EOL).
If you want to consume a library with Entity Framework from .Net Core application, you should re-target the application against .Net Framework. Here is the quote from an official source:
To use Entity Framework 6, your project has to compile against .NET Framework, as Entity Framework 6 doesn't support .NET Core. If you need cross-platform features you will need to upgrade to Entity Framework Core.
If you check sample of .Net .Core project (linked from the same document) which uses library with EF6 (exactly your case), you will see that it targets .Net Framework, not .Net Core:
<TargetFramework>net452</TargetFramework>
When doing such retargeting, you will not loose any .Net Core functionality that you are currently using. You still could use all that tasty stuff we love in .Net Core. However you limit the platform where your application could be launched to .Net Framework only. Unfortunatelly, you can't have workaround for this limitation currently, as it caused by the fact that Entity Framework is implemented only for .Net Framework. Your options are either to shift toward Entity Framework Core or to wait untill Entity Framework will become the part of .Net Standard.
In sum, to fix your current problem, change the following line in your .Net Core csproj file:
<TargetFramework>netcoreapp2.0</TargetFramework>
to
<TargetFramework>net47</TargetFramework>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With