Using an ASP.Net Core 2 Web App to call Full a library using EF6
Asked Answered
T

1

6

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?

Truthvalue answered 8/3, 2018 at 13:53 Comment(0)
S
3

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>
Subtotal answered 16/3, 2018 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.