asp.net-core - 在导航属性上使用虚拟

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

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

最佳答案

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

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

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

关于asp.net-core - 在导航属性上使用虚拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40022085/

相关文章:

asp.net core mvc 中的 CKEditor 数据绑定(bind)

c# - .NET Core 复制子文件夹以在构建和发布时输出

c# - 当 URL 包含特定查询参数时,如何绕过所有其他 MVC 路由?

c# - 模态内的多个 View

visual-studio-2015 - Visual Studio 2015 JSX/ES2015 语法高亮

c# - 调用 "Configure Data Source..."时出错。详情 : Exception has been thrown by the target of an invocation

unit-testing - VS 2015 : TF900547: The directory containing the assemblies for the Visual Studio Test Runner is not valid ''

c# - 让代码知道 ASP.NET 5 应用程序是自托管还是在 IIS 上运行的 CoreCLR 解决方案?

c# - 在.NET Core查询里面加入for

c# - Asp.Net core 如何替换配置管理器