简体   繁体   English

实体框架中的NHibernate的SchemaUpdate(代码优先)?

[英]NHibernate's SchemaUpdate in Entity Framework (code first)?

During prototyping in NHibernate, I just add a property to a class and SchemaUpdate adds a new column to the database table (when possible). 在NHibernate中进行原型制作时,我只是向类中添加一个属性,而SchemaUpdate在可能的情况下向数据库表中添加新列。

There is a similar old question: Updating database schema with Entity Framework Code First 还有一个类似的老问题: 首先使用实体​​框架代码更新数据库架构

Is there something similar in Entity Framework 6.1+? 实体框架6.1+中是否有类似内容? Or is there a 3rd-party open-source library for Entity Framework which is doing something similar to SchemaUpdate ? 还是有一个第三方框架的Entity Framework开放源代码库,其功能类似于SchemaUpdate

Yes, you can use the built-in Migrations framework (best solution) or DropCreateDatabaseIfModelChanges. 是的,您可以使用内置的Migrations框架(最佳解决方案)或DropCreateDatabaseIfModelChanges。 All of these are examples of database initializers (IDatabaseInitializer implementations), the former is more complex but more powerful, the latter is easy to set up but will drop the database when doing modifications. 所有这些都是数据库初始化程序的示例(IDatabaseInitializer实现),前者更复杂但功能更强大,后者易于设置,但在进行修改时会删除数据库。 Use the latter as this (other options exist, such as by .config file): 为此使用后者(存在其他选项,例如.config文件):

public class MyContext : DbContext
{
    static MyContext()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
    }
}

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

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