Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 250
gumby Avatar asked Dec 16 '19 14:12

gumby


People also ask

How do I update ef core tools?

Update the tools Use dotnet tool update --global dotnet-ef to update the global tools to the latest available version. If you have the tools installed locally in your project use dotnet tool update dotnet-ef . Install a specific version by appending --version <VERSION> to your command.

How do I change migration Assembly?

By default, the migrations assembly is the assembly containing the DbContext. Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project."

What is IDesignTimeDbContextFactory?

IDesignTimeDbContextFactory<TContext> Interface A factory for creating derived DbContext instances. Implement this interface to enable design-time services for context types that do not have a public default constructor.

Why doesn't my target project 'XXX' Reference Entity Framework?

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.

How to fix ‘your startup project ‘ doesn’t reference Microsoft Entity Framework Core Tools?

While running EFCore commands like Scaffold-DbContext or Add-Migration etc. application throws below error, Your startup project ‘ your project’ doesn’t reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

Which package is required for the Entity Framework Core Tools to work?

While running EFCore commands like Scaffold-DbContext or Add-Migration etc. application throws below error, Your startup project ‘ your project’ doesn’t reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work.

Why Entity Framework is not installed on project'match'?

The EntityFramework package is not installed on project 'Match'. Just update NuGet to 2.x. EF 5.0 requires it. One possibility for this error - In the Package Manager Console, there is a dropdown for 'Default Project'.


4 Answers

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

like image 164
Anthony Griggs Avatar answered Oct 21 '22 17:10

Anthony Griggs


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
like image 4
Braytiner Avatar answered Oct 21 '22 18:10

Braytiner


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.

like image 3
noobprogrammer Avatar answered Oct 21 '22 17:10

noobprogrammer


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.

If my tools defaulted to EF6 ones, I got message

Entity Framework 6 tools are running

and then I would get error message that Entity Framework is needed:

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

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

like image 2
Kadaj Avatar answered Oct 21 '22 18:10

Kadaj