添加 nuget 包:
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
继承接口:
IDesignTimeDbContextFactory<XXDbContext>
实现接口:
public XXDbContext CreateDbContext(string[] args) { var optionsBuilder = new DbContextOptionsBuilder<XXDbContext >() .UseSqlServer(_dbConnectionString, builder => builder.MigrationsAssembly(typeof(Program).Namespace)); return new XXDbContext (optionsBuilder.Options); }
efcore-cli (command bat 放在了 "ef-cli" 文件夹下面):
migrations :
cd .. rmdir /S /Q Migrations dotnet ef migrations add InitialXXDbContextMigration -c XXDbContext -o Migrations/XXDb dotnet ef migrations script -c XXDbContext -o Migrations/XXDb.sql cd ef-cli
update db:
cd .. dotnet ef database update -c XXDbContext cd ef-cli
drop db:
cd .. dotnet ef database drop -c XXDbContext cd ef-cli