首页 > 解决方案 > 在 Linq 中访问导航属性返回 - “由于对象的当前状态,操作无效”

问题描述

我不确定这是否是一个合法的错误,或者我没有看到明显的东西。我正在使用 EF Core 预览版 8。这是我的代码:

var customers = dbContext.Customers

                .Include(c => c.Addresses)
                .Include(c => c.Contacts)
                .Where(c => c.TenantId == tenant.Id);

if (!customerSearchCriteria.AccountSurname.IsEmpty())
            {
                customers = customers.Where(c => c.Contacts.Any(a =>
                    a.Surname.RemoveWhitespace().NullSafeToLower()
                        .Contains(customerSearchCriteria.AccountSurname.RemoveWhitespace().NullSafeToLower())));
            }

return customers.Skip(customerSearchCriteria.start).Take(customerSearchCriteria.length).ToList();

如果我完全删除中间的条件,查询工作正常。一旦我触摸联系人或地址属性,它将返回上述异常。有什么线索吗?

我做了一些研究,并在 github 上发现了一些关于类似问题的链接,但不确定是否是相同的情况:

https://github.com/aspnet/EntityFrameworkCore/issues/16719

标签: c#.netentity-framework.net-coreentity-framework-core

解决方案


推荐阅读