Can't add Entity Framework Core migrations to .NET Standard 2.0 project

前端 未结 2 1425
轮回少年
轮回少年 2021-01-15 21:22

I\'ve got a Solution with many projects. One of them (Domain) is a .NET Standard 2.0 project where I made my EF Core DbContext implementation for w

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-15 21:34

    Your csproj file for the class library should contain at least this package. Make sure your versions match for all your Microsoft packages. Either 2.1.0 or 2.1.1 (latest at this time)

    
    
      
        netstandard2.0
      
    
      
        
      
    
    
    

    In the Package Manager Console, set your default project set to the class library where you want the migrations. It's a drop down in the top right of the console window. Also, make sure your start-up project is the project that has the database connection info.

    If the start-up project doesn't have an instance of the DbContext you are wanting to scaffold, you will get the error because it doesn't know how to materialize a DbContext. You should have the DbContext registered in your Startup.cs like this:

    services.AddDbContext(options => options.UseSqlServer(connectionString));

    Then, run the command Add-Migration InitialCreate. This will scaffold your migration, it should add a folder to your class library.

    After, run Update-Database and it should apply the migration.

    If you are still having issues. Make sure you are running the latest SDK. https://www.microsoft.com/net/download

    More info can be found in the Docs. https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/

提交回复
热议问题