请假设这个架构:
public class Mammal { public int Id { get; set; } public string Name { get; set; } } public class Dog : Mammal { public int TailId { get; set; } public Tail Tail { get; set; } } public class Bat : Mammal { public int WingId { get; set; } public Wing Wing { get; set; } } public class Buffalo : Mammal { public virtual ICollectionHorns { get; set; } } public class Tail { public int Id { get; set; } ... } public class Wing { public int Id { get; set; } ... } public class Horn { public int Id { get; set; } ... }
现在,我的背景:
public class MyContext : DbContext { public DbSetMammals { get; set; } }
所以,我想只做一个SQL查询,并包含(并加载)所有嵌套实体,如:
var query = myContext.Mammals .IncludeIfTypeIs(d => d.Tail) .IncludeIfTypeIs (b => b.Wing) .IncludeIfTypeIs (b => b.Horns) ... ... ;
我知道我可以单独做,但我不想要因为我有很多实体,我需要最小化数据库请求.
我不想使用延迟加载,因为这也会产生许多数据库请求.
怎么实现呢?