具有自定义迁移基础的实体框架添加迁移支架

Igo*_*vas 2 entity-framework ef-code-first ef-migrations

我需要支架移植与add-migration从包管理器控制台与我的自定义迁移基地CustomMigration,其源自DbMigration

public partial class NewMigration: CustomMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

如果需要,我可以使用其他命令。我在Powershell脚本编写方面没有任何技能。我怎样才能做到这一点?

Igo*_*vas 6

我创建了新类,生成了我的迁移:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在Configuration.cs中:

internal sealed class Configuration : DbMigrationsConfiguration<EfDataAccess>
{
    public Configuration()
    {
        this.AutomaticMigrationsEnabled = false;
        CodeGenerator = new AuditMigrationCodeGenerator();
    }
}
Run Code Online (Sandbox Code Playgroud)

它将使用您的自定义代码生成器,该生成器将使用我所需的自定义迁移基础生成迁移。

有关更多信息:https : //romiller.com/2012/11/30/code-first-migrations-customizing-scaffolded-code/