简体   繁体   English

禁用实体框架中的自动迁移

[英]Disable automatic migrations in entity framework

I have my Configuration class constructor like this: 我有这样的Configuration类构造函数:

public Configuration() => AutomaticMigrationsEnabled = false;

And I have even updated my DbContent to this: 至将我的DbContent更新为:

public DatabaseContext()
    : base("DefaultConnection")
{
    Database.CommandTimeout = 900;
    Database.Log = s => Debug.WriteLine(s);
    Configuration.LazyLoadingEnabled = false;
    Database.SetInitializer(new CreateDatabaseIfNotExists<DatabaseContext>());
}

But when I try to run update-database for a specific migration: 但是,当我尝试为特定的迁移运行update-database

update-database -TargetMigration CreateOrganisation

I get this: 我得到这个:

Applying explicit migrations: [201805081508118_CreateOrganisation]. 应用显式迁移:[201805081508118_CreateOrganisation]。

Applying explicit migration: 201805081508118_CreateOrganisation. 应用显式迁移:201805081508118_CreateOrganisation。

Applying automatic migration: 201805081508117_CreateOrganisation_AutomaticMigration. 应用自动迁移:201805081508117_CreateOrganisation_AutomaticMigration。

The last one it runs, then fails because it states: 它运行的最后一个失败,因为它指出:

System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Answers' in the database. System.Data.SqlClient.SqlException(0x80131904):数据库中已经有一个名为“ Answers”的对象。

Which there is, but the actual migration looks like this: 有,但是实际的迁移看起来像这样:

public partial class CreateOrganisation : DbMigration
{
    public override void Up()
    {
        CreateTable(
                "dbo.Organisations",
                c => new
                {
                    Id = c.String(nullable: false, maxLength: 100),
                    Name = c.String(nullable: false, maxLength: 100),
                    Description = c.String(maxLength: 255),
                })
            .PrimaryKey(t => t.Id);

        AddColumn("dbo.Users", "OrganisationId", c => c.String(maxLength: 100));
    }

    public override void Down()
    {
        DropTable("dbo.Organisations");
        DropColumn("dbo.Users", "OrganisationId");
    }
}

As you can see, there is no mention of Answer in that migration, which leads me to assume it is trying to do the prior migrations.... 如您所见,在该迁移中没有提及Answer ,这使我假设它正在尝试进行先前的迁移。

Does anyone know how I can stop that? 有谁知道我能阻止它吗?


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Table renames
    modelBuilder.Entity<Criteria>().ToTable("Criteria");
    modelBuilder.Entity<Formula>().ToTable("Formulas");
    modelBuilder.Entity<IdentityRole>().ToTable("Roles");
    modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
    modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
    modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
    modelBuilder.Entity<ImageText>().ToTable("ImageText");

    // One to Many  
    modelBuilder.Entity<Criteria>().HasMany(m => m.Attributes).WithOptional().HasForeignKey(m => m.CriteriaId);
    modelBuilder.Entity<IdentityRole>().HasMany(m => m.Users).WithRequired().HasForeignKey(m => m.RoleId);
    modelBuilder.Entity<Organisation>().HasMany(m => m.Feeds).WithRequired().HasForeignKey(m => m.OrganisationId);
    modelBuilder.Entity<Organisation>().HasMany(m => m.Users).WithRequired().HasForeignKey(m => m.OrganisationId);
    modelBuilder.Entity<Group>().HasMany(m => m.Questions).WithOptional().HasForeignKey(m => m.GroupId);
    modelBuilder.Entity<Question>().HasMany(m => m.Answers).WithOptional().HasForeignKey(m => m.QuestionId);

    modelBuilder.Entity<Category>().HasMany(m => m.PriorityColours).WithRequired().HasForeignKey(m => m.CategoryId);
    modelBuilder.Entity<Category>().HasMany(m => m.Criteria).WithRequired().HasForeignKey(m => m.CategoryId);
    modelBuilder.Entity<Category>().HasMany(m => m.Feeds).WithRequired().HasForeignKey(m => m.CategoryId);
    modelBuilder.Entity<Category>().HasMany(m => m.Quotes).WithRequired().HasForeignKey(m => m.CategoryId);
    modelBuilder.Entity<Category>().HasMany(m => m.QuestionGroups).WithRequired().HasForeignKey(m => m.CategoryId);

    modelBuilder.Entity<User>().HasMany(m => m.Searches).WithRequired().HasForeignKey(m => m.UserId);
    modelBuilder.Entity<User>().HasMany(m => m.Charges).WithRequired().HasForeignKey(m => m.UserId);
    modelBuilder.Entity<Answer>().HasMany(m => m.Images).WithRequired().HasForeignKey(m => m.AnswerId);
    modelBuilder.Entity<Image>().HasMany(m => m.ImageText).WithRequired().HasForeignKey(m => m.ImageId);

    // Create our primary keys
    modelBuilder.Entity<IdentityUserLogin>().HasKey(m => m.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey(m => m.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(m => new {m.RoleId, m.UserId});
}

As requested 按照要求

Seems you are meet the same problem like me and I found the answer in below link, it is work for me and hope it also work for you: 似乎您遇到了和我一样的问题,我在下面的链接中找到了答案,这对我有用,希望对您也有用:

Update-Database tries to do an automatic migration even with automatic migrations disabled 即使禁用了自动迁移,Update-Database也会尝试进行自动迁移

The essential part of the answer I copy to here: 我将答案复制到此处的必要部分:

Entity framework will always run an automatic migration before it runs an explicit migration that has the Source property defined in its .resx file, even if AutomaticMigrationsEnabled = false. 实体框架将始终运行自动迁移,然后再运行在其.resx文件中定义了Source属性的显式迁移,即使AutomaticMigrationsEnabled = false。 An explicit migration will only have the Source property set if it is created after an automatic migration has been run. 如果显式迁移是在运行自动迁移后创建的,则仅会设置Source属性。

The upshot is that disabling automatic migrations only means that EF won't automatically upgrade your schema when it detects model changes - but it might still do an automatic migration if it needs to fill in a gap between some explicit migrations. 结果是,禁用自动迁移仅意味着EF在检测到模型更改时不会自动升级架构-但如果它需要填补某些显式迁移之间的空白,它可能仍会进行自动迁移。 To avoid this behavior, don't use a mixture of automatic migrations and explicit migrations. 为避免这种情况,请勿同时使用自动迁移和显式迁移。

And the flowing steps is what I have done to resolve this problem: 而流动的步骤是我为解决此问题所做的工作:

  • locate to the missing migration file's resx file 找到丢失的迁移文件的resx文件
  • open it and remove the source property. 打开它并删除source属性。 it is better if you can make a copy of this resx file. 最好是可以复制此resx文件。
  • run the command update-database -verbose again. 再次运行命令update-database -verbose
  • the pending migration will add to _migrationHistory table. 挂起的迁移将添加到_migrationHistory表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM