Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架核心-惰性加载不适用于getter_C#_Lazy Loading_Ef Core 2.1 - Fatal编程技术网

C# 实体框架核心-惰性加载不适用于getter

C# 实体框架核心-惰性加载不适用于getter,c#,lazy-loading,ef-core-2.1,C#,Lazy Loading,Ef Core 2.1,让我们假设这个场景 实体 public virtual List<Address> AddressHistory { get; set; } public Address Address { get { if (AddressHistory.Any()) { return AddressHistory.OrderByDescending(x => x.CreationDate).FirstOrDefaul

让我们假设这个场景

实体

public virtual List<Address> AddressHistory { get; set; }

public Address Address
{
    get
    {
        if (AddressHistory.Any())
        {
            return AddressHistory.OrderByDescending(x => x.CreationDate).FirstOrDefault();
        }

        return null;
    }
}
我得到一个空引用异常

为什么??有没有办法让它发挥作用

编辑:对于那些认为地址可能为空的人,这样做时它会起作用:

dbContext.MyEntity.Where(e => e.AddressHistory.OrderByDescending(x => x.CreationDate).FirstOrDefault().Street == "some stuff");
编辑:对于那个将它标记为重复的家伙,我认为你不理解这里的问题。请去掉标记

总而言之:

如果我使用getter=>null异常,因为子项(AddressHistory)不是延迟加载的。 如果我直接在efcore表达式中使用getter内部的代码,它就可以工作


这意味着使用getter在EFCore中不起作用。

你是对的,它不起作用。我认为永远不会

EF Core当前在导航属性(惰性或急切加载)方面存在问题。这是在下一个EF核心主要版本(3.0)中进行客户评估的原因之一。而使用这些表达式的查询只会抛出类似于EF6的异常

记住这些,不要在LINQtoEntities查询中使用这样的“helper”属性。我从OOP和可重用性的角度知道这很好,但是LINQ查询转换不能很好地进行封装,因为它基于知识,需要查看所有内容背后的代码(表达式),而不是模型映射到数据库的一部分


您已经知道了有效的解决方案,只需使用它即可。或者看看哪种方法试图解决可重用性问题。

你是对的,它不起作用。我认为永远不会

EF Core当前在导航属性(惰性或急切加载)方面存在问题。这是在下一个EF核心主要版本(3.0)中进行客户评估的原因之一。而使用这些表达式的查询只会抛出类似于EF6的异常

记住这些,不要在LINQtoEntities查询中使用这样的“helper”属性。我从OOP和可重用性的角度知道这很好,但是LINQ查询转换不能很好地进行封装,因为它基于知识,需要查看所有内容背后的代码(表达式),而不是模型映射到数据库的一部分


您已经知道了有效的解决方案,只需使用它即可。或者看看哪种方法试图解决可重用性问题。

如果没有AddressHistory,则返回null,但从未检查使用中的场景。是否调试过
AddressHistory
是否包含任何元素?另外,
LastOrDefault()
如果没有元素,则返回null。基本上,您必须使用debuggerdbContext.MyEntity.Include(“AddressHistory”).Where(e=>e.Address.Street==“一些东西”)@TonyAbrams做一个include不再懒散地加载给其他人了,AddressHistory也不是空的,显然我编辑了这个问题以提供更多信息当没有AddressHistory时,你会返回null,但你从来没有检查过使用中的场景。你是否调试过
AddressHistory
是否包含任何元素?另外,
LastOrDefault()
如果没有元素,则返回null。基本上,您必须使用debuggerdbContext.MyEntity.Include(“AddressHistory”).Where(e=>e.Address.Street==“一些东西”)@TonyAbrams做一个include对其他人来说不再是懒散的加载,地址历史记录也不是空的,显然我编辑了这个问题以提供更多信息
dbContext.MyEntity.Where(e => e.AddressHistory.OrderByDescending(x => x.CreationDate).FirstOrDefault().Street == "some stuff");