Disable automatic migrations in entity framework

0

I have my Configuration class constructor like this:

public Configuration() => AutomaticMigrationsEnabled = false;

And I have even updated my DbContent to this:

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 -TargetMigration CreateOrganisation

I get this:

Applying explicit migrations: [201805081508118_CreateOrganisation].

Applying explicit migration: 201805081508118_CreateOrganisation.

Applying automatic migration: 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.

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....

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

c#
entity-framework
asked on Stack Overflow Jun 4, 2018 by r3plica • edited Jun 4, 2018 by r3plica

1 Answer

0

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

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. An explicit migration will only have the Source property set if it is created after an automatic migration has been run.

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. 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
  • open it and remove the source property. it is better if you can make a copy of this resx file.
  • run the command update-database -verbose again.
  • the pending migration will add to _migrationHistory table.
answered on Stack Overflow Jan 23, 2019 by DennisWu • edited Jan 23, 2019 by DennisWu

User contributions licensed under CC BY-SA 3.0