A
A
Andrey Romanyuk2017-02-14 20:17:46
ASP.NET
Andrey Romanyuk, 2017-02-14 20:17:46

'System.Data.Entity.DbSet' does not contain a definition for 'Id'. What is the problem?

Visual Studio 2015
(actually Books)
17cc44224bf64a24804789be73a3152f.jpg
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>

The database is full of data.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2017-02-14
@BLek2

You have in the title of the question and have the answer
In View you pass DbSet< Book >, and expect there in the form of a Book model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question