Entity framework 将基类集合强制转换为几个派生类-EF核心

Entity framework 将基类集合强制转换为几个派生类-EF核心,entity-framework,ef-core-3.1,Entity Framework,Ef Core 3.1,我无法将基类的子实体集合强制转换为所有派生类。 让我举一个例子: public class Household { public int Id {get; set;} public virtual ICollection<Person> Persons {get; set;} } public abstract class Person { public int Id {get; set;} } public class Parent : Person {

我无法将基类的子实体集合强制转换为所有派生类。 让我举一个例子:

public class Household {
    public int Id {get; set;}
    public virtual ICollection<Person> Persons {get; set;}
}

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

public class Parent : Person {
    public int Income {get; set;}
}

public class Child : Person {
    public string School {get; set;}
}
公共类家庭{
公共int Id{get;set;}
公共虚拟ICollection人员{get;set;}
}
公共抽象类人物{
公共int Id{get;set;}
}
公共类家长:个人{
公共整数收入{get;set;}
}
公营儿童:人{
公共字符串学校{get;set;}
}
当我想要将孩子强制转换到其特定的派生类时,当我尝试选择带有孩子集合人员的家庭类的实体时,就会出现问题

我尝试了以下查询,但未成功:

var household = Context.Households.Where(h => h.Id = id)
                .Include(hp => hp.Persons).OfType<Parent>().OfType<Child>()
                .FirstOrDefault();
var househouse=Context.househouses.Where(h=>h.Id=Id)
.Include(hp=>hp.Persons).OfType().OfType()的
.FirstOrDefault();
(生成类子级不包含正确定义的错误)

var househouse=Context.househouses.Where(h=>h.Id=Id)
.Include(hp=>hp.Persons)
.Include(hpp=>hpp.Persons)
.FirstOrDefault();
(生成类父级不包含Person定义的错误)


我希望在Househouse实体上有两个派生类的集合,而不仅仅是基类。

因此,我发现EF Core工作得很好,是控制器在API调用期间丢失了子类的多态属性。 为了获得派生类,我只使用了:

var household = Context.Households.Where(h => h.Id = id)
                .Include(hp => hp.Persons).FirstOrDefault();
为了解决控制器的问题,我使用以下两个来源作为指导,和

我在上面实现了一个JsonConverter,最后得到了以下代码:

编辑类:

public class Household {
    public int Id {get; set;}
    public virtual ICollection<Person> Persons {get; set;}
}

public abstract class Person {
    public int Id {get; set;}
    public string Discriminator {get; private set;} 
    // The discriminator actually exists in the inherited parent table, 
    // I just want to attach it to my entities so I can identify to which subclass it belongs to
}

public class Parent : Person {
    public int Income {get; set;}
}

public class Child : Person {
    public string School {get; set;}
}

因此,我发现EF-Core工作得很好,是控制器在API调用期间丢失了子级的多态属性。 为了获得派生类,我只使用了:

var household = Context.Households.Where(h => h.Id = id)
                .Include(hp => hp.Persons).FirstOrDefault();
为了解决控制器的问题,我使用以下两个来源作为指导,和

我在上面实现了一个JsonConverter,最后得到了以下代码:

编辑类:

public class Household {
    public int Id {get; set;}
    public virtual ICollection<Person> Persons {get; set;}
}

public abstract class Person {
    public int Id {get; set;}
    public string Discriminator {get; private set;} 
    // The discriminator actually exists in the inherited parent table, 
    // I just want to attach it to my entities so I can identify to which subclass it belongs to
}

public class Parent : Person {
    public int Income {get; set;}
}

public class Child : Person {
    public string School {get; set;}
}
services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Converters.Add(new PersonConverter());
});