Question

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?

Was it helpful?

Solution

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>());
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top