ASP.net MVC5 Code First填充测试数据到数据库

2018-06-23 23:15:21来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

问题的产生

 

   最近在看Adam Freeman的“Pro ASP.NET MVC5”,于是在工作机上面搭建了相应的运行环境,但是在自己的机器上面只有代码,没有数据库。记得在code first中可以新建数据库并且填充数据,这样就生成数据库可以方便测试。

准备工作

 

数据库上下文

1 public class EFDbContext: DbContext
2     {
3        public DbSet<Product> Products { get; set; }
4     }
View Code

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>
View Code

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 }
View Code

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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:万能的SqlHelper,麻麻再也不用担心用什么数据库了

下一篇:tfs中如何创建团队项目及如何操作团队项目