-
Notifications
You must be signed in to change notification settings - Fork 0
CodeMigrations
Marcin Sulecki edited this page Apr 4, 2017
·
2 revisions
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MusicStoreContext, Configuration>());
MigrateDatabaseToLatestVersion to jeden z wbudowanych inicjalizatorów. Ale można również tworzyć własne.
public class MyContextInitializer : DropCreateDatabaseAlways<MyContext>
{
public override void InitializeDatabase(MyContext context)
{
base.InitializeDatabase(context);
}
protected override void Seed(MyContext context)
{
context.Persons.Add(new Person { FirstName = "John", ZipCode = "98052" });
context.Persons.Add(new Person { FirstName = "Natasha", ZipCode = "98210" });
context.Persons.Add(new Person { FirstName = "Lin", ZipCode = "98052" });
context.Persons.Add(new Person { FirstName = "Josh", ZipCode = "90210" });
context.Persons.Add(new Person { FirstName = "Maria", ZipCode = "98074" });
context.SaveChanges();
}
}
Ustawienie inicjalizatora:
public MyContext()
{
Database.SetInitializer(new MyContextInitializer());
}
context.Database.Exists();