How to get DbContext class in EF Core without bringing in EF 6?

.net-core c# entity-framework-core



I'm having a problem because Package Manager complains that "Entity Framework 6.x and Entity Framework Core commands are installed." But I don't think it's coming from the EntityFrameworkCore NuGet package. I have a website project which uses DbContext. It gets it from the Microsoft.AspNetCore.App package (the ONLY NuGet package it loads), and the website project only use the EF Core commands installed. I tried using that package in my library project, and I still get the same problem. So my problem is apparently not coming from the NuGet package.

More investigation is apparently in order...

Original question:

I'm migrating library code from .NET 4.6.2 to .NET Core 2.1. This code has references to DbContext (in System.Data.Entity). In order to pull in an equivalent .NET Core DbContext (in Microsoft.EntityFrameworkCore), I can load the Microsoft.EntityFrameworkCore.SqlServer NuGet package. The problem is that that package apparently brings in EF6 in addition to EF Core, so when I try to issue an update-database command in the Package Manager Console, it complains that "Both Entity Framework Core and Entity Framework 6 are installed".

Presumably, I can figure out a Package Manager command that works around this annoyance, but I have no (obvious) need for EF 6 and I would just as soon figure out how to bring in DbContext WITHOUT bringing in EF 6.

Any suggestions?

6/11/2018 3:25:58 AM

Accepted Answer

I figured it out. I looked at the csproj file, and there was an extraneous line, apparently left over from earlier fiddling with NuGet packages:

    Version="2.1.0" PrivateAssets="All" />

Since there is no reason for this library to deal with web code generation, I removed that line from the csproj file, and now the message about having EF 6 tools loaded has magically gone away. Go figure...

6/11/2018 3:46:09 AM

Popular Answer

This code has references to DbContext (in System.Data.Entity)

That seems to be your problem. Throw it away.

The correct DbContext lives in Microsoft.EntityFrameworkCore namespace, from the same nuget package.

Probably you should read this:

Make sure you have the old nuget package uninstalled.


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow