[英]EF Core - Include ThenInclude Interface
我正在嘗試使用.Include().ThenInclude()
加載相關數據。 我正在使用繼承,而從基類TicketEntry
派生的類可以實現IApprovable
接口:
public class Ticket
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
public ICollection<TicketEntry> TicketEntries { get; set; }
}
public abstract class TicketEntry
{
[Key]
public int Id { get; set; }
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreationDate { get; set; }
[Required]
public string UserId { get; set; }
[ForeignKey("UserId")]
public ApplicationUser User { get; set; }
public int TicketId { get; set; }
[ForeignKey("TicketId")]
public Ticket Ticket { get; set; }
}
public class Common : TicketEntry
{
[Required]
public string Description { get; set; }
}
public class CostEstimate : TicketEntry, IApprovable
{
[Required]
[Column(TypeName = "money")]
public decimal Estimate { get; set; }
public Boolean? Approved { get; set; }
public string ApprovingUserId { get; set; }
[ForeignKey("ApprovingUserId")]
public ApplicationUser ApprovingUser { get; set; }
}
現在,我想獲得Ticket
與所有它的TicketEntries和他們的User
和ApprovingUser
為實現該接口的所有TicketEntries IApprovable
。
我嘗試這樣做:
_context.Ticket
.Include(t => t.TicketEntries)
.ThenInclude(te => te.User)
.Include(t => t.TicketEntries as ICollection<IApprovable>)
.ThenInclude(ia => ia.ApprovingUser)
這不是有效的,因為它不是純屬性表達式。
我試圖查找類似的案例,但找不到任何案例。 我是否缺少某些東西,或者根本就不可能,而我正在嘗試做一些通常不應該做的事情?
即使不應該,您將如何實現呢?
不可能在EntityFramework Core 2.0或更早版本中包含派生對象。
有一個GitHub問題查詢:支持Include / ThenInclude來導航請求此功能的派生類型,該功能已添加到EntityFramework Core 2.1-preview1中。
根據2.1文檔和2.1 新增功能,您可以通過以下語法之一使用它:
var option1 = context.People.Include(p => ((Student)p).School);
var option2 = context.People.Include(p => (p as Student).School);
var option3 = context.People.Include("School");
https://blogs.msdn.microsoft.com/dotnet/2018/05/07/announcing-entity-framework-core-2-1-rc-1/
今天,我們很高興地宣布,EF Core 2.1的第一個候選版本與.NET Core 2.1 RC 1和ASP.NET Core 2.1 RC 1一起可用,可以進行廣泛的測試, 現在也可以用於生產 !
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.