简体   繁体   English

EF核心-包含然后包含接口

[英]EF Core - Include ThenInclude Interface

I am trying to load related data using .Include().ThenInclude() . 我正在尝试使用.Include().ThenInclude()加载相关数据。 I am using inheritance and my classes deriving from the base class TicketEntry can implement an interface IApprovable : 我正在使用继承,而从基类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; }
}

Now I want to get a Ticket with all it's TicketEntries and their User and the ApprovingUser for all the TicketEntries implementing the Interface IApprovable . 现在,我想获得Ticket与所有它的TicketEntries和他们的UserApprovingUser为实现该接口的所有TicketEntries IApprovable

I tried doing this: 我尝试这样做:

_context.Ticket
    .Include(t => t.TicketEntries)
        .ThenInclude(te => te.User)
    .Include(t => t.TicketEntries as ICollection<IApprovable>)
        .ThenInclude(ia => ia.ApprovingUser)

Which doesn't work since it's not a pure property expression. 这不是有效的,因为它不是纯属性表达式。

I tried to look up similar cases but couldn't find any. 我试图查找类似的案例,但找不到任何案例。 Am I missing something or is that simply not possible and I'm trying to do something you normally should not do? 我是否缺少某些东西,或者根本就不可能,而我正在尝试做一些通常不应该做的事情?

Even if one shouldn't, how would you accomplish this? 即使不应该,您将如何实现呢?

It's not possible to include derived in EntityFramework Core 2.0 or older. 不可能在EntityFramework Core 2.0或更早版本中包含派生对象。

There is an GitHub Issue Query: Support Include/ThenInclude for navigation on derived type requesting this feature, which was added to EntityFramework Core 2.1-preview1. 有一个GitHub问题查询:支持Include / ThenInclude来导航请求此功能的派生类型,该功能已添加到EntityFramework Core 2.1-preview1中。

As per 2.1 documentation and What's new in 2.1 you can use it via one of the following syntax: 根据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");

Update: build 2018 announcement & production ready 更新:建立2018公告并准备生产

https://blogs.msdn.microsoft.com/dotnet/2018/05/07/announcing-entity-framework-core-2-1-rc-1/ https://blogs.msdn.microsoft.com/dotnet/2018/05/07/announcing-entity-framework-core-2-1-rc-1/

Today, we are excited to announce that the first release candidate of EF Core 2.1 is available, alongside .NET Core 2.1 RC 1 and ASP.NET Core 2.1 RC 1, for broad testing, and now also for production use ! 今天,我们很高兴地宣布,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM