ASP.net MVC5 Code First fills in the test data to the database, asp. netmvc5

Source: Internet
Author: User

ASP.net MVC5 Code First fills in the test data to the database, asp. netmvc5

1. Problem generation

I recently read Adam Freeman's "Pro ASP. NET MVC5", so I set up the corresponding runtime environment on the work machine, but there is only code on my machine and no database. Remember to create a database and fill in data in code first so that the database can be generated for testing.

2. Preparations

Database Context

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

Web. config needs to specify the connection string of the database

<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 Console

The enable-migrations command creates a migration folder in the project. The folder also contains a Configuration. cs file, which you can edit to configure migration.

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.50 m}, 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.00 m }, 73 74 new Product75 {76 Name = "Bling-Bing King", 77 Description = "Gold-plated, diamond-studded King", 78 Category = "Chess ", 79 Price = 2.16.00m80} 81}; 82 products. forEach (m => context. products. add (m); 83 base. seed (context); 84 85} 86} 87}View Code
 
add-migration InitialCreate 
When you execute the add-migration command, the migration generates code to create a database.
Update-database
The update-database command runs the Up method to create a database, and then runs the Seed method to fill the database.

If the database runs successfully, the corresponding data is inserted.


References:
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. author Adam Freeman

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.