【问题标题】:What do navigation properties do in Entity Framework?实体框架中的导航属性有什么作用?
【发布时间】:2015-01-23 11:35:27
【问题描述】:

我使用 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; }
    }
  1. 导航属性Author的意义何在?
  2. 将属性设为virtual 有什么意义?
  3. 导航属性是否特定于实体框架?

编辑:经过更多研究,我找到了this thread,它很好地回答了我的第一个问题。

请大家帮忙解决最后两个问题吗?

【问题讨论】:

标签: c# entity-framework models


【解决方案1】:

2:如果你做虚拟导航属性,那么这个属性的延迟加载值会起作用:

public class Book
{
    //...
    public int AuthorId { get; set; }
    public virtual Author Author { get; set; }
}

它起作用:EF 为您的实体(书)创建代理并覆盖导航属性(作者)。当您加载导航属性(Author)的数据库值的实体(Book)时,不会加载。只有第一个获取导航属性(作者)的值,才会加载(http://blogs.msdn.com/b/adonet/archive/2009/12/22/poco-proxies-part-1.aspx)。

3:本质上,导航属性并不特定于实体框架。此链接到其他实体。但术语“导航属性”是 EF 特有的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多