I just upgraded a project with a code-first model from Entity Framework 6.0.2 to 6.1.0.
After the upgrade, context.Database.CompatibleWithModel(true)
returns false, so EF thinks the database is no longer compatible with the model. I haven't changed anything else, just upgraded EF.
I ran Add-Migration
to see what would happen, and EF created a big migration that seems to create an index on every foreign key property on every table:
public override void Up()
{
CreateIndex("dbo.ActivityStreams", "UserId");
CreateIndex("dbo.Users", "OfficeId");
CreateIndex("dbo.Offices", "ParentId");
CreateIndex("dbo.Rosters", "UserId");
...and many more similar lines...
I guess this is related to the new index features in EF 6.1? A bit strange, but OK.
When I Update-Database
to apply the new migration, there are errors that the indexes already exist. Looking in the database and at previous migrations, almost all the indexes do indeed already exist.
What have I done wrong here? Is there a procedure for upgrading EF?
As mentioned in other answers, this is due to an Entity Framework bug. The accepted answer of copying the DropIndex
code into the Up
method causes unnecessary work on the database. Instead, I would suggest that the correct course of action is to delete the contents of both the Up
and Down
methods as these indexes already exist and do not need to be removed.
I had the exact same issue. I fixed it by recreating the Indexes.
I've accomplished this by copy pasting the DropIndexes from the Down method in the migration into the Up method. So effectively first deleting the indexes and then recreating them. No idea why this is necessary but it fixed the problem.
Re: Your Foreign Keys being created, I found the following info at the following link:
http://blog.oneunicorn.com/2014/02/15/ef-6-1-creating-indexes-with-indexattribute/ states the following:
The ForeignKeyIndexConvention Code First convention causes indexes to be created for the columns of any foreign key in the model unless these columns already have an index specified using IndexAttribute. If you don’t want indexes for your FKs you can remove this convention:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<ForeignKeyIndexConvention>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With