标签:test base 控制器 list() head 一个数据库 数据库连接 ant meta
1 public class EFDbContext:DbContext 2 { 3 public EFDbContext() : base("EFDbContext") 4 { 5 } 6 public DbSet<Product> Product { get; set; } 7 }
<connectionStrings> <add name="EFDbContext" connectionString="Server=.;Database=SqlTest;uid=sa;pwd=123456;" providerName="System.Data.SqlClient" /> </connectionStrings>
public EFDbContext() : base("EFDbContext")
public class Product { public int ID { get; set; } public string Name { get; set; } public decimal Price { get; set; } public int Quantity { get; set; } }
1 public ActionResult Index() 2 { 3 using (EFDbContext db = new EFDbContext()) 4 { 5 var productlist = db.Product.ToList(); 6 return View(productlist); 7 } 8 }
@model List<MVCEF.Models.Product> @{ ViewBag.Title = "ProductList"; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <h2>ProductList</h2> <table class="table"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Price</th> <th>Quantity</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td>@item.ID</td> <td>@item.Name</td> <td>@item.Price</td> <td>@item.Quantity</td> </tr> } </tbody> </table> </body> </html>
标签:test base 控制器 list() head 一个数据库 数据库连接 ant meta
原文地址:https://www.cnblogs.com/ywkcode/p/10920740.html