你的位置:首页 > ASP.net教程

[ASP.net教程]ASP.net MVC5 Code First填充测试数据到数据库


1. 问题的产生

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

2.准备工作

数据库上下文

1 public class EFDbContext: DbContext2   {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

3. 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 Product23         {24           Name = "Kayak",25           Description = "A boat for one person",26           Category = "Watersports",27           Price = 275m28         },29 30         new Product31         {32           Name = "Lifejacket",33           Description = "protective and fashinonable",34           Category = "Watersports",35           Price = 48.95m36         },37 38         new Product {Name = "Soccer Ball", Description = "FIFA", Category = "Soccer", Price = 19.50m},39 40         new Product41         {42           Name = "Corner Flags",43           Description = "Give u playing field",44           Category = "Soccer",45           Price = 34.95m46         },47 48         new Product49         {50           Name = "Stadium",51           Description = "Flat-Packed,35,000-seat stadium",52           Category = "Soccer",53           Price = 79500.00m54         },55 56         new Product57         {58           Name = "Thinking Cap",59           Description = "Improve ur brain efficiency by 75%",60           Category = "Chess",61           Price = 16.00m62         },63 64         new Product65         {66           Name = "Unsteady Chair",67           Description = "Secretly give your opponent a disadvantage",68           Category = "Chess",69           Price = 29.95m70         },71 72         new Product {Name = "Human Chess Board", Description = "A fun game", Category = "Chess", Price = 75.00m},73 74         new Product75         {76           Name = "Bling-Bing King",77           Description = "Gold-plated,diamond-studded King",78           Category = "Chess",79           Price = 1200.00m80         }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