简体   繁体   中英

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 */ );

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM