我使用EF6,这些是我的POCO。
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Book
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
public int Year { get; set; }
public decimal Price { get; set; }
public string Genre { get; set; }
// Foreign Key
public int AuthorId { get; set; }
// Navigation property
public Author Author { get; set; }
}
Author
的重点是什么?virtual
?编辑:经过一番研究后,我发现了this thread,这很好地回答了我的第一个问题。
人们可以帮助解决最后两个问题吗?
答案 0 :(得分:2)
2:如果你做虚拟导航属性,那么将使用此属性的延迟加载值:
public class Book
{
//...
public int AuthorId { get; set; }
public virtual Author Author { get; set; }
}
工作正常:EF为您的实体(Book)创建代理并覆盖导航属性(Author)。当您加载实体(Book)的数据库值时,导航属性(Author)未加载。只有导航属性的第一个获取值(作者),它才会加载(http://blogs.msdn.com/b/adonet/archive/2009/12/22/poco-proxies-part-1.aspx)。
3:固有导航属性并非特定于Entity Framework。此链接指向其他实体。但术语"导航属性"特定于EF。