C# 实体框架:包括继承的类导航属性
我的班级结构如下:C# 实体框架:包括继承的类导航属性,c#,.net,entity-framework,C#,.net,Entity Framework,我的班级结构如下: public class Person { public long Id { get; set; } } public class MarketUser : Person { public ICollection<Order> Orders { get; set; } } public class AdminUser : Person { public ICollection<Request> Requests { get;
public class Person
{
public long Id { get; set; }
}
public class MarketUser : Person
{
public ICollection<Order> Orders { get; set; }
}
public class AdminUser : Person
{
public ICollection<Request> Requests { get; set; }
}
公共类人物
{
公共长Id{get;set;}
}
公共类MarketUser:Person
{
公共ICollection命令{get;set;}
}
公共类AdminUser:Person
{
公共ICollection请求{get;set;}
}
和一般方法:
public ICollection<TPerson> GetPersons<TPerson>() where TPerson : Person
{
DbSet personDbSet = GetDbSet<Person>();
ICollection<TPerson> personsCollection = personDbSet.OfType<TPerson>()
.Include("Orders")
.Include("Requests")
.ToList();
return personsCollection;
}
public ICollection GetPersons(),其中TPerson:Person
{
DbSet personDbSet=GetDbSet();
ICollection personCollection=personDbSet.OfType()
.包括(“订单”)
.包括(“请求”)
.ToList();
返回人员收集;
}
我正试着这样使用它
ICollection<Person> persons = GetPersons<Person>();
ICollection persons=GetPersons();
问题是,如何加载派生类的导航属性?在本例中,我有一个例外,因为
MarketUser
没有属性请求
,AdminUser
没有属性订单,没错,MarketUser没有属性请求,只有订单和Id属性可供MarketUser使用。要正确使用,请将订单和请求移动到Person。或者创建一个特定的GetMarketUser和GetAdminUsers没错,MarketUser没有属性请求,只有Orders和Id属性可供MarketUser使用。要正确使用,请将订单和请求移动到Person。或者创建特定的GetMarketUser和GetAdminUser