efcore 控制台迁移架构

添加 nuget 包:

  1. Microsoft.EntityFrameworkCore.Design
  2. Microsoft.EntityFrameworkCore.SqlServer
  3. 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

  

原文地址:https://www.cnblogs.com/myesn/p/efcore-schema-migartions-on-consoleapp.html