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() 方法的确是开发者的绝佳帮手。
以上是一个configuration.cs文件的实例。
注意事项:
1、guid字段必须初始化。初始化方法如下
ProductId = Guid.NewGuid()
2、可以用如下方式表示外键关联
GenreId = genres.Single(g => g.GenreName == "蔬菜").GenreId,
3、不一定要为每一个字段都赋值,但是必须为每一个required字段赋值
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字段赋值
相关文章推荐
- ASP.NET MVC 几种 Filter 的执行过程源码解析
- 史上最全的ASP.NET MVC路由配置
- (三)storm-kafka源代码走读之怎样构建一个KafkaSpout
- ASP 读写文件FSO,adodb.stream
- Asp.Net 关于FindControl方法使用
- ASP.NET Core MVC Hello World
- ASP.NET 按规定尺寸缩放图片
- 如何在asp.net页面使用css和js
- ASP.NET MVC 5通过viewdata 向视图传递多个对象
- Asp.Net Api2 过滤器的使用
- ASP.NET过滤类SqlFilter,防止SQL注入[原创]_实用技巧_脚本之家
- 中国菜刀Cknife(C刀)自定义模式秒过安全狗. jsp过狗一句话,aspx过狗一句话.
- ASP.NET Web Service如何工作(3)
- ASP.NET Web Service如何工作(2)
- ASP.NET Web Service如何工作(1)
- asp.net ashx 一般处理程序 使用async await异步直接 copy可用哦
- 请求如何进入ASP.NET MVC框架
- ASP.NET Core 1.0中的管道-中间件模式
- ASP.NET过滤类SqlFilter,防止SQL注入 原创
- 你应该知道的 asp.net webform之异步页面