dotnet ef migrations add:仅当有更改时才创建迁移文件
dotnet ef migrations add: create migration file only if there are changes
我正在使用 dotnet ef migrations add {MigrationName}
来创建新的迁移。
如果没有 Entities/Context 更改,这将创建一个具有空 Up(MigrationBuilder migrationBuilder)
和 Down(MigrationBuilder migrationBuilder)
函数的迁移。
有没有办法让 migrations add
命令跳过创建 'empty' 个文件?
或者,有没有办法检测 运行 migrations add
命令之前是否有变化?
我完全不知道它是否可行,但是如果您想手动 运行 任何脚本,例如 create/alter StoredProcedures/Trigger,空迁移也很有用。为此,您可以专门为特定目的生成空迁移。
我找到了解决这个问题的方法。
有一种方法可以 create migrations programmatically 而不是使用 CLI。
在查看 MigrationsScaffolder source code 之后,我设法修改了第一个 link 中的代码以支持我的需求:
using (var db = new MyDbContext())
{
var reporter = new OperationReporter(
new OperationReportHandler(
m => Console.WriteLine(" error: " + m),
m => Console.WriteLine(" warn: " + m),
m => Console.WriteLine(" info: " + m),
m => Console.WriteLine("verbose: " + m)));
var designTimeServices = new ServiceCollection()
.AddSingleton(db.GetService<IHistoryRepository>())
.AddSingleton(db.GetService<IMigrationsIdGenerator>())
.AddSingleton(db.GetService<IMigrationsModelDiffer>())
.AddSingleton(db.GetService<IMigrationsAssembly>())
.AddSingleton(db.Model)
.AddSingleton(db.GetService<ICurrentDbContext>())
.AddSingleton(db.GetService<IDatabaseProvider>())
.AddSingleton<MigrationsCodeGeneratorDependencies>()
.AddSingleton<ICSharpHelper, CSharpHelper>()
.AddSingleton<CSharpMigrationOperationGeneratorDependencies>()
.AddSingleton<ICSharpMigrationOperationGenerator, CSharpMigrationOperationGenerator>()
.AddSingleton<CSharpSnapshotGeneratorDependencies>()
.AddSingleton<ICSharpSnapshotGenerator, CSharpSnapshotGenerator>()
.AddSingleton<CSharpMigrationsGeneratorDependencies>()
.AddSingleton<IMigrationsCodeGenerator, CSharpMigrationsGenerator>()
.AddSingleton<IOperationReporter>(reporter)
.AddSingleton<MigrationsScaffolderDependencies>()
.AddSingleton<ISnapshotModelProcessor, SnapshotModelProcessor>()
.AddSingleton<MigrationsScaffolder>()
.BuildServiceProvider();
var scaffolderDependencies = designTimeServices.GetRequiredService<MigrationsScaffolderDependencies>();
var modelSnapshot = scaffolderDependencies.MigrationsAssembly.ModelSnapshot;
var lastModel = scaffolderDependencies.SnapshotModelProcessor.Process(modelSnapshot?.Model);
var upOperations = scaffolderDependencies.MigrationsModelDiffer.GetDifferences(lastModel, scaffolderDependencies.Model);
var downOperations = upOperations.Any() ? scaffolderDependencies.MigrationsModelDiffer.GetDifferences(scaffolderDependencies.Model, lastModel) : new List<MigrationOperation>();
if (upOperations.Count() > 0 || downOperations.Count() > 0)
{
var scaffolder = designTimeServices.GetRequiredService<MigrationsScaffolder>();
var migration = scaffolder.ScaffoldMigration(
"MyMigration",
"MyApp.Data");
File.WriteAllText(
migration.MigrationId + migration.FileExtension,
migration.MigrationCode);
File.WriteAllText(
migration.MigrationId + ".Designer" + migration.FileExtension,
migration.MetadataCode);
File.WriteAllText(migration.SnapshotName + migration.FileExtension,
migration.SnapshotCode);
}
}
我正在使用 dotnet ef migrations add {MigrationName}
来创建新的迁移。
如果没有 Entities/Context 更改,这将创建一个具有空 Up(MigrationBuilder migrationBuilder)
和 Down(MigrationBuilder migrationBuilder)
函数的迁移。
有没有办法让 migrations add
命令跳过创建 'empty' 个文件?
或者,有没有办法检测 运行 migrations add
命令之前是否有变化?
我完全不知道它是否可行,但是如果您想手动 运行 任何脚本,例如 create/alter StoredProcedures/Trigger,空迁移也很有用。为此,您可以专门为特定目的生成空迁移。
我找到了解决这个问题的方法。
有一种方法可以 create migrations programmatically 而不是使用 CLI。
在查看 MigrationsScaffolder source code 之后,我设法修改了第一个 link 中的代码以支持我的需求:
using (var db = new MyDbContext())
{
var reporter = new OperationReporter(
new OperationReportHandler(
m => Console.WriteLine(" error: " + m),
m => Console.WriteLine(" warn: " + m),
m => Console.WriteLine(" info: " + m),
m => Console.WriteLine("verbose: " + m)));
var designTimeServices = new ServiceCollection()
.AddSingleton(db.GetService<IHistoryRepository>())
.AddSingleton(db.GetService<IMigrationsIdGenerator>())
.AddSingleton(db.GetService<IMigrationsModelDiffer>())
.AddSingleton(db.GetService<IMigrationsAssembly>())
.AddSingleton(db.Model)
.AddSingleton(db.GetService<ICurrentDbContext>())
.AddSingleton(db.GetService<IDatabaseProvider>())
.AddSingleton<MigrationsCodeGeneratorDependencies>()
.AddSingleton<ICSharpHelper, CSharpHelper>()
.AddSingleton<CSharpMigrationOperationGeneratorDependencies>()
.AddSingleton<ICSharpMigrationOperationGenerator, CSharpMigrationOperationGenerator>()
.AddSingleton<CSharpSnapshotGeneratorDependencies>()
.AddSingleton<ICSharpSnapshotGenerator, CSharpSnapshotGenerator>()
.AddSingleton<CSharpMigrationsGeneratorDependencies>()
.AddSingleton<IMigrationsCodeGenerator, CSharpMigrationsGenerator>()
.AddSingleton<IOperationReporter>(reporter)
.AddSingleton<MigrationsScaffolderDependencies>()
.AddSingleton<ISnapshotModelProcessor, SnapshotModelProcessor>()
.AddSingleton<MigrationsScaffolder>()
.BuildServiceProvider();
var scaffolderDependencies = designTimeServices.GetRequiredService<MigrationsScaffolderDependencies>();
var modelSnapshot = scaffolderDependencies.MigrationsAssembly.ModelSnapshot;
var lastModel = scaffolderDependencies.SnapshotModelProcessor.Process(modelSnapshot?.Model);
var upOperations = scaffolderDependencies.MigrationsModelDiffer.GetDifferences(lastModel, scaffolderDependencies.Model);
var downOperations = upOperations.Any() ? scaffolderDependencies.MigrationsModelDiffer.GetDifferences(scaffolderDependencies.Model, lastModel) : new List<MigrationOperation>();
if (upOperations.Count() > 0 || downOperations.Count() > 0)
{
var scaffolder = designTimeServices.GetRequiredService<MigrationsScaffolder>();
var migration = scaffolder.ScaffoldMigration(
"MyMigration",
"MyApp.Data");
File.WriteAllText(
migration.MigrationId + migration.FileExtension,
migration.MigrationCode);
File.WriteAllText(
migration.MigrationId + ".Designer" + migration.FileExtension,
migration.MetadataCode);
File.WriteAllText(migration.SnapshotName + migration.FileExtension,
migration.SnapshotCode);
}
}