[英]How to query inheritance Tables in EF Core?
我的 EF Core 中有這樣的表/類
public class Member
{
public int Id {get; set;}
public string Name {get; set;}
.. other props
}
public class Employee
{
public int Id {get; set;}
public string Name {get; set;}
.. other props
}
public class Transaction {
public int Id {get; set;}
.. other props
public ICollection<TransactionDetail> Details {get; set;}
}
public class TransactionDetail{
public int Id {get; set;}
public int TransactionId {get; set;}
public decimal TransactionValue;
... other props
}
public class TransactionDetailMember: TransactionDetail{
public int MemberId;
public Member member;
... other props
}
public class TransactionDetailEmployee: TransactionDetail{
public int EmployeeId;
public Employee employee;
... other props
}
public class TransactionDetailStranger: TransactionDetail{
public string StrangerName;
... other props
}
我想記錄會員、員工和陌生人(非會員和非員工)進行的交易。 如何正確查詢此記錄,以便我可以在我的 Transaction.Details 中獲取 Member、Employee 和 Stranger 模型/表? 我應該做 3 個細節而不是一個嗎?
我目前使用.NetCore 3。
首先讓我建議重構您的 model 映射,而不是在您的TransactioDetail.cs
中具有導航屬性,讓事務本身具有到TransactionDetail
的完整導航。 添加
public int TransactionDetailId {get; set;}
public TransactionDetail TransactionDetail {get; set;}
由於您截斷了Transaction.cs
文件,我不知道它是否具有Employee
或Member
的導航屬性,但事務需要有執行它的人。 在這種情況下,如果有,您只需要
transactionRepository.Get().Include(x => x.TransactionDetail).Include(x => x.Member)
然后,您可以根據具體情況獲取詳細信息和成員或員工。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.