C# EF核心-包含接口
我正在尝试使用C# EF核心-包含接口,c#,asp.net-core,entity-framework-core,ef-core-2.0,C#,Asp.net Core,Entity Framework Core,Ef Core 2.0,我正在尝试使用.Include().thenclude()加载相关数据。我使用的是继承,从基类TicketEntry派生的类可以实现接口IApprovable: public class Ticket { [Key] public int Id { get; set; } [Required] public string Title { get; set; } public ICollection<TicketEntry> TicketEn
.Include().thenclude()
加载相关数据。我使用的是继承,从基类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; }
}
这不起作用,因为它不是一个纯属性表达式
我试图查找类似的案例,但没有找到。我是否遗漏了什么,或者这根本不可能,而我正试图做一些你通常不应该做的事情
即使不应该,您将如何实现这一点?在EntityFramework Core 2.0或更早版本中不可能包含派生的 有一个GitHub问题请求此功能,该功能已添加到EntityFramework Core 2.1-preview1中 根据,您可以通过以下语法之一使用它:
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");
更新:build 2018发布和生产就绪
今天,我们很高兴地宣布,EF Core 2.1的第一个候选版本与.NET Core 2.1 RC 1和ASP.NET Core 2.1 RC 1一起可用于广泛的测试,现在也可用于生产使用
在EF Core 2.0中不能这样做。虽然EF Core 2.1(现在在rc1中)应该是可行的,但我想评论一下,你应该写下来作为答案。我通读了2.1文档,其中包含了我需要的几个特性。所以我现在就耐心等待。你可以使用rc1。ASP.NET Core也在RC1中,并且在构建过程中从microsoft获得了一个生产准备(至少ASP.NET Core做到了,EF Core也应该做到,但它们是同步发布的)在MSDN博客文章中更新了答案,宣布EF Core 2.1 RC1为“也用于生产”谢谢,很高兴看到我最近刚刚需要的功能。我会继续,然后更新。
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");