导航属性应该是虚拟的-ef核心中不需要吗? [英] navigation property should be virtual - not required in ef core?

查看:50
本文介绍了导航属性应该是虚拟的-ef核心中不需要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得在EF 导航属性应为虚拟

public class Blog 
{  
    public int BlogId { get; set; }  
    public string Name { get; set; }  
    public string Url { get; set; }  
    public string Tags { get; set; }  

    public virtual ICollection<Post> Posts { get; set; }  
}

但是我看 EF核心,并且看不到它是虚拟的:

But I look at EF Core and don't see it as virtual:

public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }

        public ICollection<Enrollment> Enrollments { get; set; }
    }

不再需要吗?

推荐答案

虚拟在EF中从来没有必需。仅当您想要延迟加载支持时才需要它。

virtual was never required in EF. It was needed only if you want lazy loading support.

由于 EF Core尚不支持延迟加载,目前虚拟没有特殊含义。它将在何时(如果有)添加延迟加载支持(有一个计划

Since Lazy loading is not yet supported by EF Core, currently virtual have no special meaning. It would when (and if) they add lazy loading support (there is a plan for doing so).

更新:从EF Core 2.1开始,延迟加载。但是,只要您不添加 Microsoft.EntityFrameworkCore.Proxies 包,通过 UseLazyLoadingProxies 启用它,原始答案仍然适用。

Update: Starting with EF Core 2.1, Lazy loading is now supported. But as soon as you don't add Microsoft.EntityFrameworkCore.Proxies package and enable it via UseLazyLoadingProxies, the original answer still applies.

但是,如果这样做的话,事情将会完全改变最初的实施中缺少选择加入控件-它需要 全部,您的导航属性必须为虚拟 。对我来说这是没有意义的,您最好在问题修复后再使用。如果您确实需要延迟加载,请使用其他无需代理
的延迟加载,在这种情况下,虚拟也没关系。

However if you do so, the things totally change due to the lack of the opt-in control in the initial implementation - it requires all your navigation properties to be virtual. Which is no sense to me, you'd better not use that until it gets fixed. If you really need lazy loading, use the alternative Lazy loading without proxies approach, in which case again virtual doesn't matter.

这篇关于导航属性应该是虚拟的-ef核心中不需要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆