简体   繁体   中英

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

EDIT:

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?

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.

努吉特

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:

<PackageReference
    Include="Microsoft.VisualStudio.Web.CodeGeneration.Design"
    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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM