在 Entity Framework Core 2.0 中包含派生的子属性 [英] Including derived child properties in Entity Framework Core 2.0

查看:20
本文介绍了在 Entity Framework Core 2.0 中包含派生的子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Entity Framework Core 2.0,我试图构建一个查询以包含多态子实体的相关数据.

Using Entity Framework Core 2.0, I am trying to construct a query to include related data for a polymorphic child entity.

例如,给定以下类型:

    public class ParentEntity
    {
        public int Id { get; set; }

        public IList<ChildEntityBase> Children { get; set; }
    }

    public abstract class ChildEntityBase
    {
        public int Id { get; set; }
    }

    public class ChildEntityA : ChildEntityBase
    {
    }

    public class ChildEntityB : ChildEntityBase
    {
        public IList<GrandchildEntity> Children { get; set; }
    }

    public class GrandchildEntity
    {
        public int Id { get; set; }

    }

以及以下配置:

    public DbSet<ParentEntity> ParentEntities { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<ParentEntity>().HasKey(p => p.Id);
        builder.Entity<ParentEntity>().HasMany(p => p.Children).WithOne();

        builder.Entity<ChildEntityBase>().HasKey(c => c.Id);
        builder.Entity<ChildEntityBase>()
            .HasDiscriminator<string>("ChildEntityType")
            .HasValue<ChildEntityA>("a")
            .HasValue<ChildEntityB>("b");

        builder.Entity<ChildEntityA>()
            .HasBaseType<ChildEntityBase>();

        builder.Entity<ChildEntityB>()
            .HasBaseType<ChildEntityBase>()
            .HasMany(u => u.Children).WithOne();

        builder.Entity<GrandchildEntity>()
            .HasBaseType<ChildEntityBase>();

        base.OnModelCreating(builder);
    }

我正在尝试编写以下查询:

I am trying to write the following query:

        var result = this.serviceDbContext.ParentEntities
            .Include(p => p.Children)
            .ThenInclude((ChildEntityB b) => b.Children);

不幸的是,这会导致语法错误.

Unfortunately, this is resulting in a syntax error.

但是,我相信我遵循 https://github.com/中指定的语法aspnet/EntityFrameworkCore/commit/07afd7aa330da5b6d90d518da7375d8bbf676dfd

谁能建议我做错了什么?

Can anyone suggest what I'm doing wrong?

谢谢

推荐答案

此功能在 EFC 2.0 中不可用.

This functionality is not available in EFC 2.0.

它被跟踪为 #3910 查询:支持 Include/ThenInclude 用于在派生类型上导航 并且根据当前的 EFC Roadmap,计划发布 EFC 2.1 ( 下包含派生类型 项我们承诺完成的功能).

It's been tracked as #3910 Query: Support Include/ThenInclude for navigation on derived type and according to the current EFC Roadmap, it's scheduled for EFC 2.1 release (Include for derived types item under Features we have committed to complete).

这篇关于在 Entity Framework Core 2.0 中包含派生的子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆