문제

During prototyping in NHibernate, I just add a property to a class and SchemaUpdate adds a new column to the database table (when possible).

There is a similar old question: Updating database schema with Entity Framework Code First

Is there something similar in Entity Framework 6.1+? Or is there a 3rd-party open-source library for Entity Framework which is doing something similar to SchemaUpdate?

도움이 되었습니까?

해결책

Yes, you can use the built-in Migrations framework (best solution) or 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. Use the latter as this (other options exist, such as by .config file):

public class MyContext : DbContext
{
    static MyContext()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top