evilfish
evilfish

Reputation: 701

Unable to update database message when adding a DBSet in Entify Framework 6

I just have gotten to a point where I am beginning to understand how EF6 operates during update and just figured out what AutomaticMigrationsEnabled actually does. I am attempting to update a database with a new User table. I have created a new entity for the users:

[Table("Users")]
public class User
{
    <Omitted properties>
}

In the project I have been working on, it has been chosen to use explicit migrations and not having any automatic migrations. So I created a migration script to create the database:

public partial class AddingUserTable : DbMigration
{
    public override void Up()
    {
        CreateTable("dbo.Users",.... Omitted for clarity
    }

    public override void Down()
    {
        DropTable("dbo.Users");
    }
}

And update my context so I have access to it with:

public DbSet<User> Users { get; set; } 

At this point, if I execute the "Update-Database" on the NuGet package manager console it will apply the migration script but with a warning:

Applying explicit migration: 201712201003395_AddingUserTable. 
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.

It turns out that this is because I added the DBSet Users to my context. If I remove that DBSet and update the database again, the warning will not appear. If I add it, the warning will appear again. I know that the warning comes because I changed the context by adding the set and I have Automatic migrations disabled, but I already applied the changes via the explicit migration script.

What do I need to do so the Entity Framework can see that I already did the migration for it and accept the new DBSet Users without warnings?

Upvotes: 0

Views: 35

Answers (1)

sachin
sachin

Reputation: 2361

Looks like you have additional changes in your Entities that you haven't accounted for in your migration. Doing 'Add-Migration' should bring those changes in front of you.

Upvotes: 1

Related Questions