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

查看:55
本文介绍了在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.

例如,给定以下类型: / p>

For example, given the following types:

    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

However, I believe I am following the syntax as specified in 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查询:根据当前EFC Roadmap ,支持Include / ThenInclude进行派生类型的导航。 >,计划在EFC 2.1版本中发布(包含用于派生类型 -to-complete rel = noreferrer>
我们已承诺要完成的功能)。

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天全站免登陆