2

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

4
  • None of those answers solve my issue
    – r3plica
    Jun 4, 2018 at 11:51
  • Can we see how your OnModelCreating look like? Jun 4, 2018 at 11:52
  • The "duplicate" that you mentioned, doesn't work because when I try to create a new migration, it tells me I still have some outstanding. I want to commit them one by one, because they have a foreign key which doesn't exist yet. So i created separate migrations so I could populate the data after the nullable field was added, before it was turned into a foreign key.
    – r3plica
    Jun 4, 2018 at 12:01
  • You can try -verbose switch to see what exactly is going on.
    – Ivan Stoev
    Jun 4, 2018 at 13:08

1 Answer 1

3

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.
2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. Jan 23, 2019 at 5:47
  • thanks for your advice, since I am a novice and will pay more attention on how to answer questions.
    – DennisWu
    Jan 23, 2019 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.