【问题标题】:Using Virtual on navigation properties在导航属性上使用虚拟
【发布时间】:2016-10-13 13:16:09
【问题描述】:

我想知道我们何时将关键字 virtual 与导航属性一起使用(我知道它是为了延迟加载),但我正在阅读 https://docs.asp.net/en/latest/data/ef-mvc/intro.html 中的教程,该教程创建了一个 asp.net Web 应用程序核心,但他们没有使用那个虚拟了。 我检查了旧版本(MVC4、MVC5),它总是在那里,但不在核心中。 谁能给我解释一下为什么?

【问题讨论】:

    标签: asp.net-core visual-studio-2015 asp.net-core-mvc entity-framework-core lazy-loading


    【解决方案1】:

    您在实体上使用虚拟属性,因此 Entity Framework 可以在运行时创建一个代理类,该代理类继承自您的实体并将存根注入到覆盖的属性中。当您从代码中访问属性的 getter 时,此存根会执行数据库调用。

    Entity Framework Core 不支持延迟加载(目前,并且可能永远不会),因此没有理由将属性标记为虚拟。

    另请参阅:官方文档中的 Loading Related Data - Entity Framework Core 1.0.0 Documentation、GitHub 上的 Lazy Loading · Issue #3797 · aspnet/EntityFramework · GitHub 和 Stack Overflow 上的 Why use 'virtual' for class properties in Entity Framework model definitions?

    【讨论】:

    • probably never will - 为什么?你有任何消息来源吗?
    • @Maarten roadmap does mention it,但 Rowan Miller(和其他人,IIRC)提到他们是 "still undecided..." about it。这不是一个明确的“不”,但也不确定它是否会被实施。
    猜你喜欢
    • 1970-01-01
    • 2015-12-23
    • 2013-11-18
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 2014-11-01
    相关资源
    最近更新 更多