user1187282
user1187282

Reputation: 1237

Update database in Entity Framework

How can I use SetInitializer to only update the database? I don't want to drop and recreate it, because I've added only a column:

Database.SetInitializer<myDbContext>( /* Here */ );

Upvotes: 1

Views: 459

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39946

Basically you need to use DropCreateDatabaseIfModelChanges since your model classes (entity classes) have been changed.

You can also create your own custom initializer, if the above do not satisfy your requirements or you want to do some other process that initializes the database using the above initializer.

That should be something like this:

public class YourCustomInitializer : DropCreateDatabaseIfModelChanges<YourDBContext>
{
    protected override void Seed(YourDBContext context)
    {
        // sample data to be written or updated to the DB
    }
}

It is worth mentioning, you can simply add your new column to your entity, then use add-migration and update-database commands. You need to run these commands in the PackageManagerConsole.

Upvotes: 2

Related Questions