简体   繁体   中英

Your target project 'XXX' doesn't reference EntityFramework. This package is required

Can anyone point me in the direction of where I have gone wrong. I am trying to get migrations to work on EFCore with SQLite (Console App .NET Core 3.1)

Running any command such as enable-migrations or update-database gives the error;

Your target project 'XXX' doesn't reference EntityFramework. This package is for the Entity Framework Core Tools to work. Ensure your target project is correct, install the package, and try again.

Seems a bit weird to need the EntityFramework if I am using EFCore??

 <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

In my case I was getting this same exceptions because I had the EntityFrameWork package installed along with EntityFrameWorkCore. I was using the later for some older functions I had in my Library which executed SqlCommand.

Installing both packages completely scrubbed my project even after I removed the EntityFramework package. Michael Browns comment gave me a hint as I ended up having to completely reset my EntityFramework Migrations by following this suggestion: Reset Entity Framework

There's another way to do this:

We need to use a factory for the DbContext to create migration in design time.

We implement IDesignTimeDbContextFactory interface, because, for convention, when a class that implements this interface is found in the DbContext same project or in startup project, EFCore discard other ways to create the DbContext and will use the factory.

See the implementation example:

  public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
  {

    public MyDbContext CreateDbContext(string[] args)
    {
      var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();

      optionsBuilder.UseSqlServer("Your ConnectionString Here");

      return new MyDbContext(optionsBuilder.Options);
    }

After implement this, set the project that contains the DbContext as startup and default project and try to create the migrations:

//.Net Core CLI:
dotnet ef migrations Add Initial -p .\src\Data\Data.csproj -s .\src\Data\Data.csproj

//Package Manager Console
Add-Migration Initial -p .\src\Data\Data.csproj -s .\src\Data\Data.csproj

Well, this was the strange solution and to my surprise it worked!

Just update the Visual Studio from VS installer and relaunch it. This will remove all your unnecessary packages and re-install packages will solve your EF and EF core package conflicts. Then run your migration and you will see the error will go away.

Hope this helps.

I encountered this issue in project that I had both EF Core and EF6 installed.

I was constantly getting message while creating migrations:

Both Entity Framework 6 and Entity Framework Core are installed. The Entity Framework 6 tools are running. Use 'EntityFrameworkCore\\Add-Migration' for Entity Framework Core.

This was part of big solution (both .Net Framework and .Net Core ) and there is no way to make it use only one of them.

This was happening specifically on the Core part.

What I tried and possibly what I would suggest is:

  1. Try repair your visual studio installation.

  2. Setup properly " Startup project " in your Visual Studio and " Default Project " in your Package manager console .

  3. Use For EF6:

     EntityFramework\\Add-Migration <MIGRATIONNAME>

    For EF Core:

     EntityFrameworkCore\\Add-Migration <MIGRATIONNAME>

Combination of these 3 things helped me out.

As others haven't figured out, but I am sure having both EF6 and EF Core is culprit . I documented my journey here: https://github.com/dotnet/efcore/issues/27051

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