ASP.NETがEFを使用する場合のseedメソッドおよび初期データの構造


seedメソッド:
Seed()メソッドは何ですか?元の注記「This method will be called after migrating to the latest version」によると、このSeed()メソッドは私たちのmigrateが最近のバージョンになると呼び出されます.実際には、Migrationを設定した場合、Update-Database命令を実行すると、EFは自動的にこのSeed()メソッドを呼び出して実行します.
Seed()メソッドは、Update-Databaseコマンドを実行するたびに呼び出されます.だから、もう一度「ケダグダ通り3号」に参加して、Update-Databaseコマンドを実行してみると、リポジトリにそのコマンドが多く出ます.言い換えれば、同じ方法でリポジトリに資料を入れることができます.ところで、いつSeed()メソッドを使ってリポジトリに資料を詰め込むか考えなければならないかもしれません.一般に,我々がプログラムにEFを適用するのは,Seed()メソッドを用いて資料を詰め込むためではない.
しかし、私たちはきっと多くの場合、リポジトリが設立されると、自社の住所やテスト資料など、固定的で異動しにくい資料を加えることを望んでいます.だからSeed()方法は確かに開発者の絶好の手伝いです.
namespace WechatCommerceSys.DAL.Migrations
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using WechatCommerceSys.Model;

    internal sealed class Configuration : DbMigrationsConfiguration
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(WechatCommerceContext context)
        {
            const string imgUrl = "~/Images/placeholder.png";
           

            var genres = AddGenres(context);
            AddProducts(context, imgUrl, genres);
            context.SaveChanges();
            
        }


        private static void AddProducts(WechatCommerceContext context, string imgUrl, List genres)
        {
            context.Products.Add(new Product {ProductId = Guid.NewGuid(), GenreId = genres.Single(g => g.GenreName == "  ").GenreId, ProductName = "The Best Of The Men At Work", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            context.Products.Add(new Product { ProductId = Guid.NewGuid(), GenreId = genres.Single(g => g.GenreName == "  ").GenreId, ProductName = "And Justice For All", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            context.Products.Add(new Product { ProductId = Guid.NewGuid(), GenreId = genres.Single(g => g.GenreName == "  ").GenreId, ProductName = "Black Light Syndrome", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "10,000 Days", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "11i", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "1960", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "4x4=12 ", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "A Copland Celebration, Vol. I", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "A Lively Mind", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "A Matter of Life and Death", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "A Real Dead One", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "A Real Live One", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "A Rush of Blood to the Head", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "A Soprano Inspired", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });
            //context.Products.Add(new Product { ProductName = "A Winter Symphony", Genre = genres.Single(g => g.GenreName == "  "), Price = 8.99M, ProductImgUrl = imgUrl });

        }

        private static List AddGenres(WechatCommerceContext context)
        {
            var genres = new List
            {
                new Genre { GenreName = "  ", GenreId = Guid.NewGuid() },
                new Genre { GenreName = "  " , GenreId = Guid.NewGuid()},
                new Genre { GenreName = "  " , GenreId = Guid.NewGuid()},
                new Genre { GenreName = "  ", GenreId = Guid.NewGuid() },
                
            };

            genres.ForEach(s => context.Genres.Add(s));
            context.SaveChanges();
            return genres;
        }
		
    }

}

以上はconfigurationです.csファイルのインスタンス.
注意事項:
1、guidフィールドは初期化する必要があります.初期化方法は次のとおりです.
ProductId = Guid.NewGuid()
2、外部キー関連を次のように表すことができる
GenreId = genres.Single(g=>g.GenreName=="野菜").GenreId,
3、フィールドごとに値を付ける必要はありませんが、requiredフィールドごとに値を付ける必要があります.