วันพุธที่ 8 กุมภาพันธ์ พ.ศ. 2560

Asp.net MVC - Enabling Code First Migrations



การเปลี่ยนข้อมูลบน database โดย code

0. ถ้าเราต้องการย้าย database เราก็เปลี่ยน connecttion string ตรง Web.config นะ

1. พิมพ์ PM> Enable-Migrations -ContextTypeName SuaSilverShop_V2.DAL.StoreContext

2. PM> add-migration InitialDatabase

3. เพิ่มโค้ดใน 201702081048235_InitialDatabase

namespace SuaSilverShop_V2.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class initNewDatabase : DbMigration
    {
        public override void Up()
        {
            CreateTable(
            "dbo.Categories",
            c => new
            {
                ID = c.Int(nullable: false, identity: true),
                Name = c.String(),
            })
            .PrimaryKey(t => t.ID);
            CreateTable(
            "dbo.Products",
            c => new
            {
                ID = c.Int(nullable: false, identity: true),
                Name = c.String(),
                Description = c.String(),
                Price = c.Decimal(nullable: false, precision: 18, scale: 2),
                CategoryID = c.Int(),
            })
            .PrimaryKey(t => t.ID)
            .ForeignKey("dbo.Categories", t => t.CategoryID)
            .Index(t => t.CategoryID);
        }
        public override void Down()
        {
            DropForeignKey("dbo.Products", "CategoryID", "dbo.Categories");
            DropIndex("dbo.Products", new[] { "CategoryID" });
            DropTable("dbo.Products");
            DropTable("dbo.Categories");
        }
    }
}


4. ใน Migrations\Configuration.cs เพิ่ม


namespace SuaSilverShop_V2.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using Models;
    using System.Collections.Generic;

    internal sealed class Configuration : DbMigrationsConfiguration<SuaSilverShop_V2.DAL.StoreContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            ContextKey = "SuaSilverShop_V2.DAL.StoreContext";
        }

        protected override void Seed(SuaSilverShop_V2.DAL.StoreContext context)
        {
           



            var categories = new List<Category>
            {
                new Category { Name = "Clothes" },
                new Category { Name = "Play and Toys" },
                new Category { Name = "Feeding" },
                new Category { Name = "Medicine" },
                new Category { Name= "Travel" },
                new Category { Name= "Sleeping" }
            };
            categories.ForEach(c => context.Categories.AddOrUpdate(p => p.Name, c));
            context.SaveChanges();
            var products = new List<Product>
            {
                new Product { Name = "Sleep Suit", Description="For sleeping or general wear",
                Price=4.99M, CategoryID=categories.Single( c => c.Name == "Clothes").ID },
                new Product { Name = "Vest", Description="For sleeping or general wear",
                Price=2.99M, CategoryID=categories.Single( c => c.Name ==
                "Clothes").ID },
                new Product { Name = "Orange and Yellow Lion", Description="Makes a squeaking noise",
                    Price=1.99M, CategoryID=categories.Single( c => c.Name == "Play and Toys").ID },
                new Product { Name = "Blue Rabbit", Description="Baby comforter", Price=2.99M,
                CategoryID=categories.Single( c => c.Name == "Play and Toys").ID },

                new Product { Name = "3 Pack of Bottles", Description="For a leak free drink everytime",
                    Price=24.99M, CategoryID=categories.Single( c => c.Name == "Feeding").ID },
                new Product { Name = "3 Pack of Bibs", Description="Keep your baby dry when feeding",
                    Price=8.99M, CategoryID=categories.Single( c => c.Name == "Feeding").ID },
                new Product { Name = "Powdered Baby Milk", Description="Nutritional and Tasty",
                    Price=9.99M, CategoryID=categories.Single( c => c.Name == "Feeding").ID },
                new Product { Name = "Pack of 70 Disposable Nappies", Description="Dry and secure nappies with snug fit",
                    Price=19.99M, CategoryID= categories.Single( c => c.Name == "Feeding").ID },
                new Product { Name = "Colic Medicine", Description="For helping with baby colic pains", 
                    Price=4.99M, CategoryID=categories.Single( c => c.Name == "Medicine").ID },
                new Product { Name = "Reflux Medicine", Description="Helps to prevent milk regurgitation and sickness",
                    Price=4.99M, CategoryID=categories.Single( c => c.Name == "Medicine").ID },
                new Product { Name = "Black Pram and Pushchair System", Description="Convert from pram to pushchair, with raincover", 
                    Price=299.99M, CategoryID= categories.Single( c => c.Name == "Travel").ID },
                new Product { Name = "Car Seat", Description="For safe car travel",
                Price=49.99M, CategoryID= categories.Single( c => c.Name == "Travel").ID },
                new Product { Name = "Moses Basket", Description="Plastic moses basket",
                Price=75.99M, CategoryID=categories.Single( c => c.Name == "Sleeping").ID },
                new Product { Name = "Crib", Description="Wooden crib", Price=35.99M,
                CategoryID= categories.Single( c => c.Name == "Sleeping").ID },
                new Product { Name = "Cot Bed", Description="Converts from cot into bed for older children",
                    Price=149.99M, CategoryID=categories.Single( c => c.Name == "Sleeping").ID },
                new Product { Name = "Circus Crib Bale", Description="Contains sheet, duvet and bumper",
                    Price=29.99M, CategoryID=categories.Single( c => c.Name == "Sleeping").ID },
                new Product { Name = "Loved Crib Bale", Description="Contains sheet,duvet and bumper",
                    Price=35.99M, CategoryID=categories.Single( c => c.Name == "Sleeping").ID }
                };
                products.ForEach(c => context.Products.AddOrUpdate(p => p.Name, c));
                context.SaveChanges();


        }
    }

}




5. พิมพ์ PM> Update-Database จบ

ไม่มีความคิดเห็น:

แสดงความคิดเห็น