How to add a table (change data model) to the EF4 Context without lose of data
-
06-11-2019 - |
문제
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; }
}
올바른 솔루션이 없습니다
제휴하지 않습니다 StackOverflow