我創建了一個生成我的遷移的新類:
public class AuditMigrationCodeGenerator : CSharpMigrationCodeGenerator
{
protected override void WriteClassStart(string @namespace, string className, IndentedTextWriter writer, string @base, bool designer = false, IEnumerable<string> namespaces = null)
{
@base = @base == "DbMigration" ? "AuditMigration" : @base;
var changedNamespaces = namespaces?.ToList() ?? new List<string>();
changedNamespaces.Add("Your.Custom.Namespace");
base.WriteClassStart(@namespace, className, writer, @base, designer, changedNamespaces);
}
}
在Configuration.cs:
internal sealed class Configuration : DbMigrationsConfiguration<EfDataAccess>
{
public Configuration()
{
this.AutomaticMigrationsEnabled = false;
CodeGenerator = new AuditMigrationCodeGenerator();
}
}
它會使用自定義的代碼生成器,它產生與我期望的自定義遷移基地的遷移。
欲瞭解更多信息:https://romiller.com/2012/11/30/code-first-migrations-customizing-scaffolded-code/
感謝的答案,但是這就是我們現在做的,這是麻煩的,因爲我們做每天約10遷移新項目。我們需要在每次遷移時進行自定義初始化和邏輯處理,例如審計。所以這對我們來說不是可以接受的選擇,並且希望沒有定製遷移的腳手架來防止初始化錯誤。 –