问题
I have 2 projects in my solution, I have a project with Entity Framework Core installed:
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?
回答1:
I found the solution here: http://obscureproblemsandgotchas.com/development/c/dotnet-core-ef-migration-not-working/
In short, edit your csproj file, and add to your PropertyGroup
section following entry:
<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
回答2:
Do you have multiple projects? If yes then you have to make the host project as startup project from solution explorer and set the project as default (which project has DBContext) in PMC. Then run Add-Migration
command.
回答3:
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
回答4:
"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.
回答5:
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!
回答6:
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!
回答7:
Try to set your web project again as startup project and this warning should go. (Right click on web project > Set as startUp project)
回答8:
You could solve this by using below commands
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
回答9:
In order for the migration tool to work, I had to add the Microsoft.VisualStudio.Web.CodeGeneration.Design
NuGet package as well.
回答10:
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
回答11:
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
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.
回答12:
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);
}
}
回答13:
Set the project with entities as the Startup project and run the scaffolding command. it worked for me. Don't forget to set the revert the startup project after.
来源:https://stackoverflow.com/questions/52536588/your-startup-project-doesnt-reference-microsoft-entityframeworkcore-design