Visual Studio 2015
( в действительности Books )
HomeController:
BookContext db = new BookContext();
// GET: Home
public ActionResult Index()
{
return View(db.Books);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
Index.cshtml:
@{
ViewBag.Title = "Index";
Layout = "~/Views/_Layout.cshtml";
}
@Model WebApplication4.Models.Books
<h2>Книга № @Model.Id</h2>
@Html.DisplayForModel()
Book ( Model ):
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public int Price { get; set; }
}
BookContext ( Model ) :
public class BookContext :DbContext
{
public BookContext() : base("BookContext")
{ }
public DbSet<Book> Books { get; set; }
public DbSet<Purchase> Purchases { get; set; }
}
WebConfig:
<add name="BookContext" connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Bookstore.mdf;Integrated Security=True; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" /> </connectionStrings>
База данных заполнена данными.