您的位置:首页 > 编程语言 > ASP

ASP.NET使用EF时的seed方法以及初始数据的构造

2016-03-25 16:19 435 查看
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<WechatCommerceSys.DAL.WechatCommerceContext>
{
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<Genre> 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<Genre> AddGenres(WechatCommerceContext context)
{
var genres = new List<Genre>
{
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字段赋值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: