尝试在 EF Core 中使用“ThenInclude”时出现异常
Exception when trying to use `ThenInclude` in EF Core
我正在使用 Entity Framework Core 和存储库模式,但遇到一个问题。
我有 类 Customer
、Company
和 Email
,其中隐藏了与此处无关的内容,如下所示:
public class Email
{
public int EmailId { get; protected set; }
public string Address { get; protected set; }
public string Description { get; protected set; }
public Email(string address, string description)
{
if (string.isNullOrEmpty(address))
throw new ArgumentException(nameof(address));
if (string.isNullOrEmpty(description))
throw new ArgumentException(nameof(description));
this.Address = address;
this.Description = description;
}
protected Email() { }
}
public class Company
{
public int CompanyId { get; protected set; }
public IList<Email> Emails { get; set; }
}
public class Customer
{
public int CustomerId { get; protected set; }
public Company Company { get; set; }
}
映射设置为 Customer
和 Company
之间存在一对一关联,而 Company
和 [= 之间存在一对多关联=14=].
然后在 CustomersRepository
上创建了以下方法:
public IEnumerable<Customer> GetAll()
{
return _context.Set<Customer>()
.Include(x => x.Company)
.ThenInclude(x => x.Emails)
as IEnumerable<Customer>;
}
现在 ThenInclude
块出问题了。如果我尝试使用这种方法,我最终会得到一个 execption 说 source
是空的。
我检查了所有内容,但没有发现任何错误。看来一切都写对了。
重点是:我有实体 A
、B
、C
,因此 A
具有 B
和 [=23] 之一=] 有很多 C
,当我检索 A
时,我需要关联所有内容。
我在这里做错了什么?为什么我会收到此异常?
这似乎与 Github https://github.com/aspnet/EntityFramework/issues/2274
上的错误报告有关
它被报告为 IOE,然后报告已修复,然后又作为 NRE 返回,就像您的异常一样。这个问题说它已经再次修复,但我不确定是什么版本,我也不知道你目前使用的是什么版本。
(在 github 重现中搜索了 ThenInclude 问题——有一个 TON。)
听起来不稳定。远离它。您可以通过直接指定包含的完整路径来简单地完全避免该问题
muhCompaniesOrWhatevs.Include(x => x.Company.Emails);
我正在使用 Entity Framework Core 和存储库模式,但遇到一个问题。
我有 类 Customer
、Company
和 Email
,其中隐藏了与此处无关的内容,如下所示:
public class Email
{
public int EmailId { get; protected set; }
public string Address { get; protected set; }
public string Description { get; protected set; }
public Email(string address, string description)
{
if (string.isNullOrEmpty(address))
throw new ArgumentException(nameof(address));
if (string.isNullOrEmpty(description))
throw new ArgumentException(nameof(description));
this.Address = address;
this.Description = description;
}
protected Email() { }
}
public class Company
{
public int CompanyId { get; protected set; }
public IList<Email> Emails { get; set; }
}
public class Customer
{
public int CustomerId { get; protected set; }
public Company Company { get; set; }
}
映射设置为 Customer
和 Company
之间存在一对一关联,而 Company
和 [= 之间存在一对多关联=14=].
然后在 CustomersRepository
上创建了以下方法:
public IEnumerable<Customer> GetAll()
{
return _context.Set<Customer>()
.Include(x => x.Company)
.ThenInclude(x => x.Emails)
as IEnumerable<Customer>;
}
现在 ThenInclude
块出问题了。如果我尝试使用这种方法,我最终会得到一个 execption 说 source
是空的。
我检查了所有内容,但没有发现任何错误。看来一切都写对了。
重点是:我有实体 A
、B
、C
,因此 A
具有 B
和 [=23] 之一=] 有很多 C
,当我检索 A
时,我需要关联所有内容。
我在这里做错了什么?为什么我会收到此异常?
这似乎与 Github https://github.com/aspnet/EntityFramework/issues/2274
上的错误报告有关它被报告为 IOE,然后报告已修复,然后又作为 NRE 返回,就像您的异常一样。这个问题说它已经再次修复,但我不确定是什么版本,我也不知道你目前使用的是什么版本。
(在 github 重现中搜索了 ThenInclude 问题——有一个 TON。)
听起来不稳定。远离它。您可以通过直接指定包含的完整路径来简单地完全避免该问题
muhCompaniesOrWhatevs.Include(x => x.Company.Emails);