Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration

ef-migrations entity-framework

Question

Using migration (EF 5.0) and code-first, I get a weird effect:

Some models I made have GUID-based primary keys. (BTW: I find it crucial that SQL Server utilizesNEWSEQUENTIALID() , which in the most recent iteration appears to be the default setting)

I eventually turned on migrations. The most of the code I contributed to the initial migration is.Index() as required.

I see the following error while calling update-database after deleting the database:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration.

A tryAutomaticMigrationsEnabled = true This required no alterations or additions and was effective!

Although I don't want to,AutomaticMigrationsEnabled I also tried erasing the database one again by callingupdate-database after thatadd-migration . I acquired a further migration that didn't appear to alter anything (see below). Additionally, I tried inserting these lines at the base of the first migration, but nothing changed.

One of the prototypes:

[Table(Speaker.TABLENAME)]
public class Speaker : BaseModel
{
    public const String TABLENAME = "Speaker";

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    [Required]
    [MaxLength(50, ErrorMessage = "Name must be 50 characters or less")]
    public string Name { get; set; }
}

The migration's initial code:

public partial class InitialCreate : DbMigration
{
    public override void Up()
    {
        // [...]
        CreateTable(
            "dbo.Speaker",
            c => new
                {
                    Id = c.Guid(nullable: false, identity: true),
                    Name = c.String(nullable: false, maxLength: 50),
                })
            .PrimaryKey(t => t.Id)
            .Index(t => t.Name, true, false);   // added manually: unique Name
        // [...]
    }
}

internal sealed class Configuration : DbMigrationsConfiguration<MyProject.Repositories.DBContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(MyProject.Repositories.DBContext context)
    {
        // ...
    }
}

The code added by add-migration is shown below: It doesn't appear to do anything novel; perhaps I'm overlooking something.

public partial class UnneccessaryMigration : DbMigration
{
    public override void Up()
    {
        // isn't this the exact same code from InitialMigrations?
        AlterColumn("dbo.Speaker", "Id", c => c.Guid(nullable: false, identity: true));
        // ...
    }

    public override void Down()
    {
        //...
        AlterColumn("dbo.Speaker", "Id", c => c.Guid(nullable: false));
    }
}

I'm therefore curious: What did I do to confuse migrations? How can I fix it with just one first migration, then?

Solution: For me, the following solution worked:

  1. According to this description, I destroyed the database and all migrations: https://stackoverflow.com/a/11679386/3168401
  2. Executed Add-Migration Initial and Enable-Migrations
  3. combined mine homemade. Index() modifies the file. Now, Update-Database functions once more, repeatedly destroying the database as well.
1
69
5/21/2018 9:00:35 AM

Accepted Answer

I also tried deleting the database again, called update-database and then add-migration. I ended up with an additional migration that seems not to change anything (see below)

I believe you completed the last task first based on the information above. If you fleeUpdate database before Add-migration it won't apply your migration schemas to the database updates. You must first add the migration before executing the update command.

Utilizing the package manager console, try them in this order.

PM> Enable-migrations //You don't need this as you have already done it
PM> Add-migration Give_it_a_name
PM> Update-database
109
1/9/2014 2:23:05 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow