无法理解此 EF Core 2 关系行为
Cannot understand this EF Core 2 relationships behaviour
首先使用 EF 核心 2 代码,我有 4 个实体:
客户有一组联系人和地址。
客户有一个 PaymenTerm 和 PaymentTerm 有一组客户(不需要,因为我不想按 PaymentTerm 搜索)。
我想检索所有客户的联系方式和地址以及他的付款期限:
var customers = await _context.Customers
.Include(c => c.Contacts)
.Include(c => c.Addresses)
.Include(c => c.PaymentTerm)
.ToListAsync();
这些是 dbcontext 配置:
客户:
x.HasMany(e => e.Contacts)
.WithOne(e => e.Customer)
.OnDelete(DeleteBehavior.Cascade);
x.HasMany(e => e.Addresses)
.WithOne(e => e.Customer)
.OnDelete(DeleteBehavior.Cascade);
付款期限:
x.HasMany(e => e.Customers)
.WithOne(e => e.PaymentTerm)
.OnDelete(DeleteBehavior.Restrict);
问题:查询没有 return 任何记录。这是生成的 SQL 查询:
SELECT [c].[CustomerId], [c].[Cif], [c].[CommercialName], [c].[Created], [c].[CreatedBy], [c].[CustomerName], [c].[Deleted], [c].[DeletedBy], [c].[DeletedDate], [c].[Modified], [c].[ModifiedBy], [c].[PaymentTermId], [c].[TaxFree], [c].[Vat], [t].[PaymentTermId], [t].[Created], [t].[CreatedBy], [t].[Days], [t].[Deleted], [t].[DeletedBy], [t].[DeletedDate], [t].[Modified], [t].[ModifiedBy], [t].[PaymentTermName]
FROM [Customers] AS [c]
INNER JOIN (
SELECT [p].[PaymentTermId], [p].[Created], [p].[CreatedBy], [p].[Days], [p].[Deleted], [p].[DeletedBy], [p].[DeletedDate], [p].[Modified], [p].[ModifiedBy], [p].[PaymentTermName]
FROM [PaymentTerms] AS [p]
WHERE [p].[Deleted] = 0
) AS [t] ON [c].[PaymentTermId] = [t].[PaymentTermId]
WHERE [c].[Deleted] = 0
ORDER BY [c].[CustomerId]
没有添加联系人和地址的加入,也没有添加记录 returned。
如果我删除 .Include(c => c.PaymentTerm)
,有联系人和地址的客户 return 没问题。
拜托,你能告诉我我做错了什么吗?
您的查询正在执行内部联接,而您必须使用左联接。您可以使用 DefaultIfEmpty
.
来实现
首先使用 EF 核心 2 代码,我有 4 个实体:
客户有一组联系人和地址。 客户有一个 PaymenTerm 和 PaymentTerm 有一组客户(不需要,因为我不想按 PaymentTerm 搜索)。
我想检索所有客户的联系方式和地址以及他的付款期限:
var customers = await _context.Customers
.Include(c => c.Contacts)
.Include(c => c.Addresses)
.Include(c => c.PaymentTerm)
.ToListAsync();
这些是 dbcontext 配置: 客户:
x.HasMany(e => e.Contacts)
.WithOne(e => e.Customer)
.OnDelete(DeleteBehavior.Cascade);
x.HasMany(e => e.Addresses)
.WithOne(e => e.Customer)
.OnDelete(DeleteBehavior.Cascade);
付款期限:
x.HasMany(e => e.Customers)
.WithOne(e => e.PaymentTerm)
.OnDelete(DeleteBehavior.Restrict);
问题:查询没有 return 任何记录。这是生成的 SQL 查询:
SELECT [c].[CustomerId], [c].[Cif], [c].[CommercialName], [c].[Created], [c].[CreatedBy], [c].[CustomerName], [c].[Deleted], [c].[DeletedBy], [c].[DeletedDate], [c].[Modified], [c].[ModifiedBy], [c].[PaymentTermId], [c].[TaxFree], [c].[Vat], [t].[PaymentTermId], [t].[Created], [t].[CreatedBy], [t].[Days], [t].[Deleted], [t].[DeletedBy], [t].[DeletedDate], [t].[Modified], [t].[ModifiedBy], [t].[PaymentTermName]
FROM [Customers] AS [c]
INNER JOIN (
SELECT [p].[PaymentTermId], [p].[Created], [p].[CreatedBy], [p].[Days], [p].[Deleted], [p].[DeletedBy], [p].[DeletedDate], [p].[Modified], [p].[ModifiedBy], [p].[PaymentTermName]
FROM [PaymentTerms] AS [p]
WHERE [p].[Deleted] = 0
) AS [t] ON [c].[PaymentTermId] = [t].[PaymentTermId]
WHERE [c].[Deleted] = 0
ORDER BY [c].[CustomerId]
没有添加联系人和地址的加入,也没有添加记录 returned。
如果我删除 .Include(c => c.PaymentTerm)
,有联系人和地址的客户 return 没问题。
拜托,你能告诉我我做错了什么吗?
您的查询正在执行内部联接,而您必须使用左联接。您可以使用 DefaultIfEmpty
.