EF Core继承与延迟加载问题及方案遗留疑问
EF Core 延迟加载与继承关联问题
问题场景
基于.NET 6.0 + EF Core 6.0.21 + Microsoft.EntityFrameworkCore.SqlServer 6.0.19开发时,遇到实体继承与延迟加载的关联异常。定义的实体类如下:
public class Pool { public int Id { get; set; } public virtual Profile? Profile { get; set; } public int? AProfileId { get; set; } public int? BProfileId { get; set; } } public abstract class Profile { public Profile() { } public int Id { get; set; } } public class AProfile : Profile { public AProfile() { } public virtual Pool? Pool { get; set; } } public class BProfile : Profile { public BProfile() { } public virtual Pool? Pool { get; set; } } public class CProfile : Profile { public CProfile() { } // CProfile 无 Pool 属性 } // 其他 Profile 子类
通过HasOne配置AProfile、BProfile与Pool的一对一关系并启用延迟加载后,出现两个问题:
- 检索
Pool对象时,aPool.Profile为空(但AProfileId字段有值),bPool.Profile却能正常加载; - 尝试将
AProfile关联到Pool.Profile时抛出异常:
System.InvalidOperationException: 'The instance of entity type 'AProfile' cannot be tracked as the entity type 'BProfile' because the two types are not in the same hierarchy.'
已尝试的解决方案
经@sorosh_sabz建议,修改Pool类实现代理属性并重配关系:
public class Pool { public int Id { get; set; } public Profile? Profile { get { return ORMAProfileAccessor as Profile ?? ORMBProfileAccessor; } set { switch (value) { case AProfile aProfile: ORMAProfileAccessor = aProfile; break; case BProfile bProfile: ORMBProfileAccessor = bProfile; break; default: throw new InvalidCastException("Invalid profile type"); } } } internal virtual AProfile? ORMAProfileAccessor { get; set; } internal virtual BProfile? ORMBProfileAccessor { get; set; } } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<AProfile>().HasOne(P => P.Pool).WithOne(Q => Q.ORMAProfileAccessor).HasForeignKey<Pool>("AProfileId").IsRequired(false); modelBuilder.Entity<BProfile>().HasOne(P => P.Pool).WithOne(Q => Q.ORMBProfileAccessor).HasForeignKey<Pool>("BProfileId").IsRequired(false); modelBuilder.Entity<Pool>().Ignore(P => P.Profile); }
该方案能正确更新数据库外键,但检索Pool对象时Profile字段仍为空;单独检索Profile实例时,其关联的Pool字段可正常加载。
疑问
需要额外配置什么才能让Pool的Profile字段实现自动填充?
内容的提问来源于stack exchange,提问作者f soltanzadeh
相关产品推荐
相关产品推荐

