1) 放入 Context
public class NewCustomerOrderDBInitializer : DropCreateDatabaseIfModelChanges<NewCustomerOrderDB>
{
protected override void Seed(NewCustomerOrderDB context)
{
context.Products.Add(new Product() {ProductID = 1, Title= "Dear John", Description="drama", Price=11.99 });
context.Products.Add(new Product() { ProductID = 2, Title = "Inception", Description="action", Price = 12.99 });
context.Customers.Add(new Customer() { CustomerID = 1, FirstName = "John", LastName = "Doe", Company = "Freelancer", Email = "johndoe@pixnet.net", EmailCheck = "johndoe@pixnet.net" });
context.SaveChanges();
base.Seed(context);
}
}
2) Global.asax (用剛才在Context中建立的新initializer ,取代原來的initializer)
protected void Application_Start() :use the one we just created, replace the old one
{
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<myFirstProject.Models.NewCustomerOrderDB>());
Database.SetInitializer(new NewCustomerOrderDBInitializer());