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());
           

arrow
arrow
    文章標籤
    MVC Seed codefirst
    全站熱搜
    創作者介紹
    創作者 chilliwack425 的頭像
    chilliwack425

    無聊的部落格

    chilliwack425 發表在 痞客邦 留言(0) 人氣()