【发布时间】:2017-01-04 19:24:12
【问题描述】:
我正在尝试学习 ASP.NET MVC 5,但我一开始就卡住了 :) 我有简单的数据库
-
dbo.Books(int Id(PK), nvarchar(50) Title, int Author(foreignkey)) -
dbo.Author(int ID(PK), nvarchar(50) 名称)
我正在尝试使用实体框架显示书籍列表。我的模型看起来像:
public class Author
{
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<Book> Books { get; set; }
}
public class MovieContext :DbContext
{
public DbSet<Books> Books{ get; set; }
public DbSet<Author> Authors { get; set; }
}
public class Book
{
public int Id { get; set; }
public int Author { get; set; }
public string Title { get; set; }
[ForeignKey("Author")]
public virtual Author Authorr { get; set; }
}
当我转到BooksController/index 时,它会显示书籍列表,但它显示的不是作者姓名,而是 ID。我试图更改代码
@Html.DisplayFor(modelItem => item.Author)
为此:
@Html.DisplayFor(modelItem => item.Authorr.Name)
在 Index.cshtml 类中
但现在我收到一个错误:
无效的对象名称 dbo.Authors。
我的表被称为Author,我不知道它为什么要寻找Authors。
我应该更改什么以显示正常名称而不是 Author 的 Id?
【问题讨论】:
标签: asp.net asp.net-mvc entity-framework razor