简体   繁体   English

在实体框架中更新数据库

[英]Update database in Entity Framework

How can I use SetInitializer to only update the database?如何使用SetInitializer仅更新数据库? 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.基本上你需要使用DropCreateDatabaseIfModelChanges因为你的 model 类(实体类)已经改变。

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.值得一提的是,您可以简单地将新列添加到实体中,然后使用add-migration 和 update-database命令。 You need to run these commands in the PackageManagerConsole.您需要在 PackageManagerConsole 中运行这些命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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