120

I have 2 projects in my solution, I have a project with Entity Framework Core installed:

enter image description here

And in the other ASP.NET Web API project I have these packages:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.5.0.2" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights" version="2.5.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.5.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.5.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Web" version="2.5.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.5.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.5.1" targetFramework="net461" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.4" targetFramework="net461" />
  <package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net461" />
  <package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.4" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.4" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.4" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.4" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.4" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net461" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="11.0.1" targetFramework="net461" />
  <package id="System.Diagnostics.DiagnosticSource" version="4.4.1" targetFramework="net461" />
  <package id="WebGrease" version="1.6.0" targetFramework="net461" />
</packages>

When I run Add-Migration in PMC:

Your startup project 'API' 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.

I installed Microsoft.EntityFrameworkCore.Design in the startup project instead of the data project that will contain all the entities and now it works, is this how the project should be setup?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
el_pup_le
  • 11,711
  • 26
  • 85
  • 142
  • 3
    If your DB context project is not the same as entry project you need to add entry project location when adding migration, e.g. `dotnet ef migrations add initialMigration -s ../MainProjectLocation/ --context MyDbContext`. – milan Sep 27 '18 at 12:23
  • Do you have multiple projects? If yes then you have to make the host project as startup project and set the project as default (which project has DBContext) in PMC – Vivek Nuna Sep 27 '18 at 17:44
  • @milan I'm not using dotnet ef, I'm using Microsoft.EntityFrameworkCore – el_pup_le Sep 27 '18 at 23:49
  • @CodeCaster should I install all packages in the startup project? Why does it want the design package installed in the startup project? – el_pup_le Sep 27 '18 at 23:52
  • 1
    You need Microsoft.EntityFrameworkCore.Tools for running PM-Console commands, but the specific packages like Microsoft.EntityFrameworkCore.Design was necessary for Microsoft.EntityFrameworkCore.Tools to work, I think it was normal for it, anyway, you have resolved this issue. – Jack Zhai Oct 04 '18 at 09:44
  • @el_pup_le I got the exact same message on my project. Following the instructions on the message, I ran `Install-Package Microsoft.EntityFrameworkCore.Design` in the PM Console and everything started working fine. – nam Dec 24 '19 at 23:35
  • 28
    So much misinformation in this thread, save yourself the trouble and remove `all` from the package reference in the `.csproj` file from `Microsoft.EntityFrameworkCore.Design` for your infrastructure project. – user692942 Sep 16 '20 at 15:48
  • Removing`all` is most definitely **not** the right solution to this! You don't want `Microsoft.EntityFrameworkCore.Design` to get published along with your code (see [this link](https://stackoverflow.com/questions/42574122/purpose-of-package-microsoft-entityframeworkcore-design) for details). To fix this, make sure you set the project containing your db context as **both the default and start up project in your Package Manager Console** and **make sure you override your `OnConfiguring` method in your db context so a default connection string is supplied**. – ZakDaniels99 Mar 23 '22 at 17:35
  • 7
    I simply had to do a Rebuild All (Ctrl+Shift+B) – Zimano Jun 22 '22 at 13:14
  • Try this solution worked for me [here](https://stackoverflow.com/a/60021738/13797046) – Wesley Masunika Jan 06 '23 at 12:38

29 Answers29

102

I found the solution here.

In short, edit your csproj file, and add to your PropertyGroup section the following entry:

<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
John Smith
  • 7,243
  • 6
  • 49
  • 61
astrowalker
  • 3,123
  • 3
  • 21
  • 40
67

None of the options worked for me. But the one I tried on this topic worked: EF Core 3 design time migrations broken by Microsoft.EntityFrameworkCore.Design DevelopmentDependency

I just commented out the following after importing the package to the Data project:

<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.5">
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  <!--<PrivateAssets>all</PrivateAssets>-->
</PackageReference>

Thank you so much Microsoft by breaking the existing projects every time when you release a new .NetCore update!

thus
  • 1,326
  • 1
  • 14
  • 23
  • 1
    This also worked for me. However, what are further consequences of this? – Cubelaster Sep 27 '20 at 08:29
  • 1
    I am using a solution where efcore is a seperate project, your answer worked for me – RamWill Nov 07 '20 at 15:54
  • 3
    I replaced "all" with "none" instead which I think is more appropriate, but the answer helped me find the solution, thanks! Here is some more info for anyone who wana investigate further how PrivateAssets work: https://learn.microsoft.com/en-us/dotnet/core/tools/csproj – Ludvig W Jan 02 '21 at 17:14
  • Thanks, this resolved it for me too! Here's a related issue on github: https://github.com/dotnet/Scaffolding/issues/711 – Alex Feb 16 '21 at 13:17
  • GenerateRuntimeConfigurationFiles tag not resolved my issue but this works on me. – xpirrr May 08 '21 at 15:00
  • Hahaha, I'm using this answer for the third time... Thank you! – Cubelaster Aug 05 '21 at 08:39
  • Thanks for this answer. I faced this problem in my .net core 5 web app. I commented PrivateAssets tags in class_lib.csproj file. After that it worked. – Yusif Karimov Aug 23 '22 at 11:54
  • This is the correct answer.. definitely should be marked as such – James Woodley Oct 24 '22 at 20:09
  • [The reason](https://github.com/dotnet/efcore/issues/23168#issuecomment-722650360) for the `` and `` being added is that the assemblies of the designer tools should not be deployed but might still [needed in testing](https://github.com/dotnet/efcore/issues/11437#issuecomment-376606517) hence they can't be declared as development packages. – Kim Apr 17 '23 at 06:53
48

"Your 'project name' does not refer to your startup project Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Make sure your startup project is correct, install the package and try again."

If you get this error, try 'Build > Clean Solution' in your project and then try running your command again.

This worked in my project. Or you may want to look at the documentation.

I use .Net Core 3.1 version in my project.

  • 6
    Again, doing this isn't a solution. It just ends up adding EF assemblies into a startup project that should be entirely separate from the infrastructure (separation of concerns). [This is the correct answer](https://stackoverflow.com/a/62663918/692942) which removes the `all` in the infrastructure assembly `.csproj` file. – user692942 Sep 16 '20 at 15:46
  • 2
    I have not done anything mentioned in other answers, just Cleaning the solution and the main project is using .net core 3.1. It worked for me. – Muhammad Zaheer Nazir Apr 07 '21 at 12:49
  • This didn't solve the issue for me. The error still occurs. – Matt Jun 13 '22 at 12:20
33

Do you have multiple projects? If yes then you have to make the host project a startup project from solution explorer and set the project as default (which project has DBContext) in PMC. Then run the Add-Migration command.

Package Manager Console screenshot:

enter image description here

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • Thank you, that worked for me. What I would like to know is why this is necessary even when I am running the Add-Migration command from the package manager console with the right project selected as target. – Nicole May 13 '21 at 12:41
  • @Nicole please check this link https://stackoverflow.com/a/12031921/6527049 – Vivek Nuna May 13 '21 at 13:36
  • @viveknuna What exactly do you mean 'set the project as default in PMC'? – GoWiser Jun 17 '22 at 13:08
  • 1
    @GoWiser I have added a screenshot to show the default project in my answer, you can check. – Vivek Nuna Jun 17 '22 at 14:05
  • @viveknuna I was just curious, I use CMD/Bash only, not powershell. I have no idea if your solution works. I upvoted your comment. None of the solutions found on stackoverflow for multi project (database design in one project, usage of database design in another) worked for me - I also need to setup devops pipeline for automatic deployment of migrations. – GoWiser Jun 23 '22 at 12:42
  • This solution worked for my where I had EF related code in a separate class library. – jorel Jun 25 '22 at 09:00
  • Did that together with cleaning solution and it works as a charm, thank you! – Andrey Tagaew Feb 15 '23 at 17:04
25

Please make sure You have installed below packages. I was also getting same error and Installed below packages and it worked for me.

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
Kiran Kardel
  • 267
  • 3
  • 3
  • 3
    Thank you, this worked for me: 'dotnet add package Microsoft.EntityFrameworkCore.Design' source https://learn.microsoft.com/en-us/ef/core/get-started/install/ – mruanova Jun 12 '20 at 16:23
  • 6
    Again, doing this isn't a solution. It just ends up adding EF assemblies into a startup project that should be entirely separate from the infrastructure (separation of concerns). [This is the correct answer](https://stackoverflow.com/a/62663918/692942) which removes the `all` in the infrastructure assembly `.csproj` file. – user692942 Sep 16 '20 at 15:45
  • what about `Microsoft.EntityFrameworkCore`, is that required also ? – joedotnot Jul 04 '21 at 12:49
12

I got this error because Visual Studio defaulted to using entity framework core rather than old-school entityframework for .NET Framework: entity framework 6. This was my solution:

EntityFramework\Update-Database

Or reference the version explicitly:

EntityFramework6\Update-Database

Also worth checking the right project is selected in the Package Manager Console. That likes to sneak back to other projects half the time!

noelicus
  • 14,468
  • 3
  • 92
  • 111
11

There is absolutely NO reason to install Microsoft.EntityFrameworkCore.Design to your API project while having a separate Data project.

Quick solution

When using add-migration, just add a parameter to set the -StartUpProject to your Data proj (by default I presume your startup project is the API proj).


Example add-migration command below:

add-migration {migration_name} -StartUpProject {your_data_proj_name} -project {your_data_proj_name} -v

Example update-database command below:

update-database -StartUpProject {your_data_proj_name} -project {your_data_proj_name} -v

I hope this help you guys

PS: more info about the add-migration params can be found here

Andonov
  • 331
  • 4
  • 10
9

Click on startup project name and remove this:

<PrivateAssets>all</PrivateAssets> 

from package reference Microsoft.EntityFrameworkCore.Design.

Phoenix
  • 1,045
  • 1
  • 14
  • 22
6

If you want to do migrations without adding EF references in your services, your library with data access needs a way to construct an instance of your DbContext. You can do this by implementing IDesignTimeDbContextFactory, example:

public class MyContextFactory : IDesignTimeDbContextFactory<MyContext>
{
    public MyContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
        optionsBuilder.UseSqlServer("Data Source=MyDatabase");

        return new MyContext(optionsBuilder.Options);
    }
}
6

You could solve this by using below commands

dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
juagicre
  • 1,065
  • 30
  • 42
Kavinda Senarathne
  • 1,813
  • 13
  • 15
  • 2
    Adding the package doesn't help keep the EF assembly separate, which is entirely possible the issue is the use of `all` in the `csproj` is hiding the assembly from being accessed by the startup project. – user692942 Sep 16 '20 at 15:42
  • Thanks for providing the only answer that isn't IDE specific >:( – pixelpax Oct 24 '22 at 17:29
5

Try to set your web project again as startup project and this warning should go. (Right click on web project > Set as startUp project)

Anuj Shukla
  • 271
  • 3
  • 7
4

It works for me when i deleted Private Assets in .csproj or

EFCore.Design

Not making the private assets property all through the properties of the package

gurkan
  • 884
  • 4
  • 16
  • 25
  • Worked for me. However, the Nuget package has now this yellow triangle, as something is not right. I'm wondering why is that. It's not a big deal but it bothers me a bit. – luizs81 Nov 10 '21 at 07:12
3

This happen for me when

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

solution was EntityFrameworkCore\Add-Migration

Abdullah Tahan
  • 1,963
  • 17
  • 28
2

Set the project with entities as the Startup project and run the scaffolding command. it worked for me. Don't forget to revert the startup project after.

Shivanka
  • 723
  • 2
  • 8
  • 21
  • Funny Microsoft bug as usual. Setting the project as startup project in the solution resolve the isssue. Thank you @Shivanka – hatem87 Mar 14 '21 at 10:14
2

I had the same issue while I had more than one project in the solution. the mistake I was doing was I had not selected the concerned project as the Default project in Package Manager Console. So check if you have selected the same project as default in Package Manager Console in which you want to run migration command.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
waqar haider
  • 75
  • 10
2

For anyone stuck on this issue with EF Core Design installed, I simply unloaded and reloaded the project to solve the problem.

Right click the project in the Solution Explorer, unload the project, then right click again to reload.

Now it should recognize Entity Framework Core Design.

SidGabriel
  • 205
  • 1
  • 3
  • 12
2

If you have these packages

  1. Microsoft.EntityFrameworkCore.
  2. Microsoft.EntityFrameworkCore.Tools
  3. Microsoft.EntityFrameworkCore.SqlServer
  4. Microsoft.EntityFrameworkCore.

Set your web project as 'Startup' then open in Visual Studio toolbar Tools > Nuget Package Manager > Console run this commands in order

EntityFrameworkCore\Enable-Migrations
EntityFrameworkCore\add-migration migration-name
EntityFrameworkCore\update-database

that means you will use EntityFrameworkCore to add migrations.

1

In order for the migration tool to work, I had to add the Microsoft.VisualStudio.Web.CodeGeneration.Design NuGet package as well.

PJ3
  • 3,870
  • 1
  • 30
  • 34
1

More a workaround than a solution, I ended up unloading the startup project from the solution. Ran the migration, added the project back to the solution. I don't expect to have many migrations, so it worked for me. What baffles me is that I have a solution with similar features and I didn't have the same issue.

JayJay
  • 562
  • 12
  • 24
1

you need to install the design package of Microsoft EntityFrameworkCore. run this command

dotnet add package Microsoft.EntityFrameworkCore.Design

this will include package in .csproj file

balan kugan
  • 99
  • 1
  • 3
1

I had this same problem and I found out that the cause was that I installed Misoft.EntityFrameworkCore, Misoft.EntityFrameworkCore.SqlServer and Misoft.EntityFrameworkCore.Tools all in my Data Layer (in one project), remember we have multiple projects (Multiple Tier), and i set one of the projects as the StartUp. So my solution was to uninstall Misoft.EntityFrameworkCore.SqlServer and Misoft.EntityFrameworkCore.Tools from the Data layer (that is the project where I have my DbContext) and install them (Misoft.EntityFrameworkCore.SqlServer and Misoft.EntityFrameworkCore.Tools) in the Dependencies of StartUp project.

HOW TO INSTALL Right click on the Dependencies in the StartUp project, select: Manage NuGet Packages.. Click install and search the above two packages.

Projects in the Application Image of how it should look after following the below steps

ouflak
  • 2,458
  • 10
  • 44
  • 49
Cee
  • 11
  • 2
1

In my case I had multiple projects set to start up, once I set it to a single project the Add-Migration command worked:

enter image description here

And:

enter image description here

Telimaktar
  • 31
  • 2
0

First install the "microsoft.entityframeworkcore.design" better to install it using the nuget from the Package Manager Console and the command is: Install-Package Microsoft.EntityFrameworkCore.Design -Version x.x.x the link to the nuget package is this link

Then rebuild the solution after installing the required DLL, you might need to install other dependencies after that, but at least you will be done with the "microsoft.entityframeworkcore.design" dependency

ibr
  • 319
  • 1
  • 5
  • 19
0

Please make sure you have installed the same version of the packages below:

  1. Microsoft.EntityFrameworkCore.SqlServer
  2. Microsoft.EntityFrameworkCore.Design

I was also getting the same error and re-installed the packages with the same version and it worked for me.

Pluto
  • 2,900
  • 27
  • 38
Toqeer Yousaf
  • 382
  • 4
  • 9
0

What solved it for me was to have the same version of Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore.SqlServer packages, which in my case was 2.1.14

Edit 1: This was solved using a

<Project Sdk="Microsoft.NET.Sdk.Web">

on

<TargetFramework>net5.0</TargetFramework>
Salahuddin Ahmed
  • 4,854
  • 4
  • 14
  • 35
0

There is one more reason to get this error (even if you changed PrivateAssets) - if your startup project is not referenced to data access project. So if you just created new projects it can be possible case

Dmitri
  • 1
0

Another cause of this is simply I was completely missing the package Microsoft.EntityFrameworkCore.Tools package, which became uninstalled somehow.

It's worth noting that the error I got for this was similar but different and was "Your target project 'XXX' 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."

Installing it got me back on track.

Mike Upjohn
  • 1,251
  • 2
  • 16
  • 38
0

I installed MySql.EntityFrameworkCore to use MySQL with Entity Framework, but I encountered this error. I attempted to fix the issue by installing various packages, but eventually realized that the only necessary package was Microsoft.EntityFrameworkCore.Tools.

After installing "EntityFrameworkCore.Tools" through the Nuget interface, it is important to save changes by pressing "Ctr + Shift + S" to apply them, otherwise the solution may not work correctly.

Nong Tinh
  • 1
  • 1
0

This issue occurred randomly and none of the other solutions resolved it. Deleting the bin, obj, and root .VS folder, temp directory, and restarting VS worked for me.

To clean temp directory:

rd /Q /S "%localappdata%\temp"
nh43de
  • 813
  • 11
  • 11