Your startup project doesn't reference Microsoft.EntityFrameworkCore.Design

后端 未结 15 734
天涯浪人
天涯浪人 2021-02-01 12:51

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:

相关标签:
15条回答
  • 2021-02-01 13:14

    "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.

    0 讨论(0)
  • 2021-02-01 13:19

    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.

    0 讨论(0)
  • 2021-02-01 13:21

    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);
        }
    }
    
    0 讨论(0)
  • 2021-02-01 13:22

    You could solve this by using below commands

    dotnet tool install --global dotnet-ef
    dotnet add package Microsoft.EntityFrameworkCore.Design
    
    0 讨论(0)
  • 2021-02-01 13:24

    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.

    0 讨论(0)
  • 2021-02-01 13:28

    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!

    0 讨论(0)
提交回复
热议问题