导航属性在Entity Framework中的作用是什么?

时间:2015-01-23 11:35:28

标签: c# entity-framework models

我使用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. 的重点是什么?
  4. 导航属性是否特定于Entity Framework?
  5. 编辑:经过一番研究后,我发现了this thread,这很好地回答了我的第一个问题。

    人们可以帮助解决最后两个问题吗?

1 个答案:

答案 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。