ASP.net MVC5 Code First填充测试数据到数据库
2018-06-22 07:07:17来源:未知 阅读 ()
问题的产生
最近在看Adam Freeman的“Pro ASP.NET MVC5”,于是在工作机上面搭建了相应的运行环境,但是在自己的机器上面只有代码,没有数据库。记得在code first中可以新建数据库并且填充数据,这样就生成数据库可以方便测试。
准备工作
数据库上下文
1 public class EFDbContext: DbContext 2 { 3 public DbSet<Product> Products { get; set; } 4 }
web.config 需要指定数据库的链接字符串
<connectionStrings> <add name="EFDbContext" connectionString="Data Source=.\mysql;Initial Catalog=SportsStore;Integrated Security=True;User ID=sa;Password=123123" providerName="System.Data.SqlClient" /> </connectionStrings>
Nuget 控制台
enable-migrations命令将在项目中创建一个迁移文件夹。同时文件夹中包含一个Configuration.cs文件,你可以编辑该文件来配置迁移。
1 using System.Collections.Generic; 2 using SportsStore.Domain.Entities; 3 4 namespace SportsStore.Domain.Migrations 5 { 6 using System; 7 using System.Data.Entity; 8 using System.Data.Entity.Migrations; 9 using System.Linq; 10 11 internal sealed class Configuration : DbMigrationsConfiguration<SportsStore.Domain.Concrete.EFDbContext> 12 { 13 public Configuration() 14 { 15 AutomaticMigrationsEnabled = false; 16 } 17 18 protected override void Seed(SportsStore.Domain.Concrete.EFDbContext context) 19 { 20 var products = new List<Product> 21 { 22 new Product 23 { 24 Name = "Kayak", 25 Description = "A boat for one person", 26 Category = "Watersports", 27 Price = 275m 28 }, 29 30 new Product 31 { 32 Name = "Lifejacket", 33 Description = "protective and fashinonable", 34 Category = "Watersports", 35 Price = 48.95m 36 }, 37 38 new Product {Name = "Soccer Ball", Description = "FIFA", Category = "Soccer", Price = 19.50m}, 39 40 new Product 41 { 42 Name = "Corner Flags", 43 Description = "Give u playing field", 44 Category = "Soccer", 45 Price = 34.95m 46 }, 47 48 new Product 49 { 50 Name = "Stadium", 51 Description = "Flat-Packed,35,000-seat stadium", 52 Category = "Soccer", 53 Price = 79500.00m 54 }, 55 56 new Product 57 { 58 Name = "Thinking Cap", 59 Description = "Improve ur brain efficiency by 75%", 60 Category = "Chess", 61 Price = 16.00m 62 }, 63 64 new Product 65 { 66 Name = "Unsteady Chair", 67 Description = "Secretly give your opponent a disadvantage", 68 Category = "Chess", 69 Price = 29.95m 70 }, 71 72 new Product {Name = "Human Chess Board", Description = "A fun game", Category = "Chess", Price = 75.00m}, 73 74 new Product 75 { 76 Name = "Bling-Bing King", 77 Description = "Gold-plated,diamond-studded King", 78 Category = "Chess", 79 Price = 1200.00m 80 } 81 }; 82 products.ForEach(m => context.Products.Add(m)); 83 base.Seed(context); 84 85 } 86 } 87 }
add-migration InitialCreate
当您执行add-migration命令时,迁移将生成代码用来创建数据库。
update-database
update-database命令运行Up方法来创建数据库,然后运行Seed方法来填充数据库。
运行成功,数据库就会有对应的数据插入。
参考文献:
1.Getting Started with Entity Framework 6 Code First using MVC 5 https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
2.Pro ASP.NET MVC5. 作者 Adam Freeman
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:RPC 的概念模型与实现解析
下一篇:VB的判断语句和循环语句
- asp.net源程序编译为dll文件并调用的实现过程 2020-03-29
- Asp.net MVC SignalR来做实时Web聊天实例代码 2020-03-29
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据 2020-03-29
- Asp.Net中WebForm的生命周期 2020-03-29
- ASP.NET使用Ajax返回Json对象的方法 2020-03-23
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash