简体   繁体   English

实体框架中的导航属性有什么作用?

[英]What do navigation properties do in Entity Framework?

I use EF6 and these are my POCO's. 我使用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. What is the point of the navigation property Author ? 导航属性Author什么?
  2. What is the point of making the property virtual ? 使该属性virtual什么意义?
  3. Are navigation properties specific to Entity Framework? 导航属性是否特定于Entity Framework?

EDIT: after a bit more research I've found this thread , which nicely answer my first question. 编辑:经过更多的研究,我发现了这个线程 ,很好地回答了我的第一个问题。

Can people help with the last two questions please? 人们可以帮您解决最后两个问题吗?

2: if you do virtual navigation properties then will work lazy load value of this property: 2:如果您执行虚拟导航属性,则该属性的延迟加载值将起作用:

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

It work: EF create proxy to your entity (Book) and overrides navigation property (Author). 它的工作原理是:EF为您的实体(书)创建代理,并覆盖导航属性(作者)。 When you load entity (Book) of the database value of the navigation property (Author) is not load. 当您加载数据库的实体(Book)时,导航属性(Author)的值也不会加载。 Only the first get value of navigation property (Author), it will be load ( http://blogs.msdn.com/b/adonet/archive/2009/12/22/poco-proxies-part-1.aspx ). 只有导航属性(Author)的第一个获取值会被加载( http://blogs.msdn.com/b/adonet/archive/2009/12/22/poco-proxies-part-1.aspx )。

3: inherently navigation properties is no specific to Entity Framework. 3:固有的导航属性并不特定于实体框架。 This link to other entities. 此链接到其他实体。 But term "navigation properties" is specific to EF. 但是术语“导航属性”特定于EF。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架中的导航属性是什么 - What are the Navigation Properties in Entity Framework 在什么情况下我需要实体框架中的外键和导航属性 - In what scenarios do I need foreign keys AND navigation properties in entity framework 如何使用Entity Framework保存对导航属性的更改 - How do you save changes to navigation properties using Entity Framework 如何在实体框架中更新导航属性? - How do you update navigation properties in entity framework? 在实体框架中使用存储过程,如何获取实体以填充其导航属性? - Using a stored procedure in entity framework, how do I get the entity to have its navigation properties populated? 处置上下文后,如何从实体框架(数据库优先)返回包含导航属性的模型? - How do I return a Model from Entity Framework (Database First) that includes the Navigation Properties after the context is disposed? 如何阻止MVC4 ApiController序列化实体框架导航属性? - How do I prevent an MVC4 ApiController from serializing Entity Framework Navigation Properties? 实体框架6.1:代码优先导航属性没有在数据库中定义的对应键 - Entity Framework 6.1: Code First navigation properties that do not have corresponding keys defined in database 实体框架 SqlQuery:如何实现 implement.where.orderby 扩展并包含导航属性? - Entity Framework SqlQuery : how to do implement .where .orderby extend and include navigation properties? 包含导航属性时,如何阻止 Entity Framework Core 创建“自引用循环”? - How do I stop Entity Framework Core from creating “Self Referencing Loops” when including navigation properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM