【发布时间】:2021-12-21 04:28:50
【问题描述】:
我有以下四张表:
客户
Id
FirstName
...
消费点
Id
Address
...
发票
Id
InvoiceNumber
CustomerId
ConsumptionPointId
...
合约账户
Id
ContractAccountNumber
CustomerId
ConsumptionPointId
IsCurrentDelivery
...
我想获取发票的 ContractAccountNumber。
是否可以在这两者之间创建某种关系以直接访问 Invoice 的 ContractAccount(s)?
目前我正在做类似的事情:
invoice.Customer.ContractAccounts
.Where(ca => ca.ConsumptionPoint == invoice.ConsumptionPoint &&
ca.IsCurrentDelivery == true).FirstOrDefault();
update 在 SQL 中我只需使用多个条件进行连接:
SELECT i.Id AS InvoiceId, ca.Id AS ContractAccountId,
ca.ContractAccountNumber
FROM Invoices i
LEFT JOIN ContractAccounts ca
ON i.ConsumptionPointId = ca.ConsumptionPointId
AND i.CustomerId = ca.CustomerId
WHERE ca.IsCurrentDelivery = 1
更新 2:
基本上我只是想摆脱 Where-Clause 中的 ca.ConsumptionPoint == invoice.ConsumptionPoint 并想在关系中定义它。
实际上这是一个多对多的关系:一个 Invoice 可以链接到多个 ContractAccounts(通过不同的 Customer/ConsumptionPoint 组合),一个 ContractAccount 可以链接到多个 Invoices。有没有办法告诉 .net 基于两个自定义列的组合建立多对多关系?
【问题讨论】:
-
您可以在数据库中创建一个 VIEW 以仅公开具有所需条件的数据,并在您的实际实体和您的 VIEW 之间创建 1-1 关系
-
@Cleptus 带有 VIEW 的想法听起来很有趣。我认为这应该有效。我会试试这个。是的,应该是
ca.IsCurrentDelivery感谢您的更正。
标签: c# .net-core entity-framework-core fluent