使用 Entity Framework 4.6.1 的 .Net 4.6.1 应用程序使用 ToList() 抛出 Null 异常。 X.ToList 中的对象 X 不为 null

发布于 2025-01-10 06:10:53 字数 1048 浏览 0 评论 0原文

我们有一个应用程序,当我们执行 ToList() 时,它会导致空异常。我们将 Entity Framework 6 与 c# 和 .Net 4.6.1 结合使用。

变量名称已重命名为通用名称。如果我造成语法不一致,我深表歉意。它已经工作了很长时间,我刚刚 看到了这个问题。代码如下:

var mYOrdList = new List();

mYOrdList.Add(order);
var mYOrdQuery = mYOrdList.AsQueryable<Order>();

下面的链式语句包含 ToList(),它会抛出 null 异常。请注意 .ToOrderDetail() 的返回值不是空值!

var orderDetailViewModel = mYOrderQry.ToOrderDetail().ToList().First();
             

下面的代码片段显示了 ToOrderDetail() 的开头:

public static IQueryable<OrderDetailViewModel> ToOrderDetail(
        this IQueryable<Order> orders)
    {
        return orders.Select(order => new OrderDetailViewModel
    {
    orderId = order.orderID,
    ...

我注意到,当查看手表中“order”对象中的数据时,该对象中的某些集合不可见并显示“X”。相关消息是-“EntityFrameworkDynamic Proxies 的元数据 - OurDataLayer 是 无效的。如果您正在执行小型转储,则可以通过使用堆收集新的小型转储并再次评估表达式来解决此问题。”

我在 c:\windows\minidump 文件夹中没有找到小型转储。这对应于我的 % systemroot% 目录

有人可以指教吗

? 肯

We have an application which is causing a null exception when we do a ToList(). We use Entity Framework 6 with c# and .Net 4.6.1.

The variable names were renamed to be generic. My apology if I caused any inconsistency in the syntax. It has worked for a long time and I have just
seen this issue with it. Code is shown below:

var mYOrdList = new List<Order>();

mYOrdList.Add(order);
var mYOrdQuery = mYOrdList.AsQueryable<Order>();

The chained statement below includes the ToList(), which throws the null exception.Please note that the return value of .ToOrderDetail() is not a null!

var orderDetailViewModel = mYOrderQry.ToOrderDetail().ToList().First();
             

This snippet below shows the beginning of the ToOrderDetail():

public static IQueryable<OrderDetailViewModel> ToOrderDetail(
        this IQueryable<Order> orders)
    {
        return orders.Select(order => new OrderDetailViewModel
    {
    orderId = order.orderID,
    ...

I noticed that when looking at the data in the "order" object in the watch, some of the collections within this object were not visible and showed an "X". The related message was- "The metadata for EntityFrameworkDynamic Proxies - OurDataLayer is
invalid. If you are doing a minidump, you may be able to fix this problem by collecting a new minidump with heap and evaluating the expressiion again."

I did not find a minidump in my c:\windows\minidump folder. That corresponds to my %systemroot% directory.

Can someone please advise?

Thank you,
Ken

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱要勇敢去追 2025-01-17 06:10:54

ToOrderDetail 返回IQueryable,所以它当然不是#null。问题是,ToList 将尝试执行 IQueryable,这就是您的问题所在。空引用异常可能是查询 Order 以填充 DTO 的代码尝试引用尚未预先加载的相关导航属性。由于即使没有进行急切加载调用,EF 也会使用跟踪实体填充引用,因此这种情况可能会出现不一致的情况。如果 DbContext 已经在跟踪一个订单问题中的相关实例,则该调用将起作用,但如果您加载另一个订单,其中一个或多个引用尚未被跟踪且未预先加载,如果延迟加载未启用/不可用,那么您将获得空引用。当 DTO 更改并且有人只是引用导航属性而没有检查该属性在调用该方法的所有情况下是否已预先加载时,这些错误往往会显现出来。

首先运行 mYOrderQry.ToList() 并查找将由 DTO Select 语句引用的 null 导航属性。要修复此问题,您需要确保这些引用已预先加载。

ToOrderDetail returns IQueryable, so of course it isn't #null. The issue is that ToList will attempt to execute the IQueryable which is where your problem lies. The Null Reference exception will likely be that your code that is querying the Order to populate the DTO is attempting to reference a related navigation property that has not been eager loaded. This can appear to occur inconsistently because of the way EF will populate references with tracked entities even without an eager load call being made. If the DbContext was already tracking the related instances in questions for one Order, the call will work, but if you load another order where one or more references aren't already tracked and weren't eager loaded, then you get a Null Reference if lazy loading isn't enabled/available. These bugs tend to manifest when a DTO changes and someone just references a navigation property without checking that the property was eager loaded in all cases where that method is called.

Start by running mYOrderQry.ToList() and looking for null navigation properties that would be referenced by your DTO Select statement. To fix it, you need to ensure that these references are eager loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文