I have an MVC3 project I created using the Code First paradigm and it works. I created a model inheriting from DBContext, and I can write to the table and all that. I need to add new table and change my model and I using DropCreateDatabaseIfModelChanges<T> But, I lose all my data, due to EF4 will drop end create new DB.
public class MyDbInitializer : DropCreateDatabaseIfModelChanges< MyDb >
Can I use different interface like UpdateDatabaseIfModelChanges<T> if any exist?
My model looks like this:
[Table("Person")]
public class Person
{
public int ID { get; set; }
public string name { get; set; }
public string country { get; set; }
public string gender { get; set; }
}
public class MyDb: DbContext
{
public DbSet<Person> People { get; set; }
}
Then I call:
Database.SetInitializer(new MyDbInitializer ());
public class MyDbInitializer : DropCreateDatabaseIfModelChanges< MyDb >
{
protected override void Seed(MyDb context)
And this I want to create:
[Table("Person")]
public class Person
{
public int ID { get; set; }
public string name { get; set; }
public string country { get; set; }
public string gender { get; set; }
}
[Table("NewTable")]
public class NewTable
{
public int ID { get; set; }
public string name { get; set; }
public string position { get; set; }
public string description { get; set; }
}
public class MyDb: DbContext
{
public DbSet<Person> People { get; set; }
public DbSet< NewTable > NewTable { get; set; }
}
Check Code first migrations and EF 4.3 beta where database upgrade is supported with
MigrateDatabaseToLatestVersioninitializer. No RTM version support this yet.