是否可以附加修改初始迁移的索引?

Rud*_*cek 0 entity-framework ef-code-first ef-migrations entity-framework-5

我在我的项目中使用实体框架代码第一种方法.我在包管理器控制台中使用enable-migrations语句创建了模型并启用了迁移.我的上下文类包含映射配置和关系

modelBuilder.Configurations.Add(new PostMap());
modelBuilder.Configurations.Add(new UserMap());
Run Code Online (Sandbox Code Playgroud)

我的迁移文件中的Up方法201302261054023_InitialCreate.cs包含所有表定义,作为通过DbSet <Type>属性指定的上下文.

我在自定义数据库初始化程序中更改了InitializeDatabase方法,因为我想在数据库初始化期间迁移到特定的迁移.

public void InitializeDatabase(T context)
{
    bool databaseExists = context.Database.Exists();
    if (!databaseExists)
        context.Database.CreateIfNotExists();

    var configuration = new Configuration();
    var migrator = new DbMigrator(configuration);
    migrator.Update("InitialCreate");
}
Run Code Online (Sandbox Code Playgroud)

可以修改201302261054023_InitialCreate类中的Up方法来添加另一个索引

CreateTable(
        "dbo.City",
        c => new
            {
                Id = c.Int(nullable: false, identity: true),
                Name = c.String(maxLength: 450),
            })
        .PrimaryKey(t => t.Id);
Run Code Online (Sandbox Code Playgroud)

CreateTable(
        "dbo.City",
         c => new
            {
                Id = c.Int(nullable: false, identity: true),
                Name = c.String(maxLength: 450),
            })
        .PrimaryKey(t => t.Id)
        .Index(t=>t.Name,true);
Run Code Online (Sandbox Code Playgroud)

没有附加另一个迁移?

Rud*_*cek 5

在挖掘了大量与迁移相关的网页之后,我发现修改初始迁移只有在我将数据库更新为"零迁移"(空数据库)时才能帮助我.如果没有迁移到零数据库,我可以添加另一个迁移并在新迁移中创建索引

public partial class InitialIndexes : DbMigration
{
    public override void Up()
    {
        CreateIndex("City", "Name", true);
    }

    public override void Down()
    {
        DropIndex("City", new[] {"Name"});
    }
}
Run Code Online (Sandbox Code Playgroud)