C# 具有条件关系的实体框架

C# 具有条件关系的实体框架,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,是否有可能在实体框架中建立基于条件的关系?我的模型看起来像这样 公共类文档 { 公共int Id{get;set;} 公共字符串名称{get;set;} 公共所有者类型所有者类型{get;set;} public int OwnerId{get;set;} 公共虚拟组织OrganizationOwner{get;set;} 公共虚拟用户UserOwner{get;set;} } 公共枚举所有者类型 { 组织=1, 用户=2 } 公营班级组织 { 公共int Id{get;set;} 公共字符串名

是否有可能在实体框架中建立基于条件的关系?我的模型看起来像这样

公共类文档
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共所有者类型所有者类型{get;set;}
public int OwnerId{get;set;}
公共虚拟组织OrganizationOwner{get;set;}
公共虚拟用户UserOwner{get;set;}
}
公共枚举所有者类型
{
组织=1,
用户=2
}
公营班级组织
{
公共int Id{get;set;}
公共字符串名称{get;set;}
//[特定于组织的其他财产]
公共虚拟列表文档{get;set;}
}
公共类用户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
//[特定于用户的其他属性]
公共虚拟列表文档{get;set;}
}
因此,我想设置一个关系,以便在OwnerType==OwnerType.Organization时自动填充文档实例的OrganizationOwner属性,在OwnerType==OwnerType.User时填充UserOwner属性

是否可以先在EntityFramework-Code中建立这种关系?在映射中类似这样的内容

EntityTypeConfiguration.HasOptional(d=>d.OrganizationOwner)
.WithMany(o=>o.Documents)
.HasForeignKey(d=>d.OwnerId)
其中(d=>d.OwnerType==OwnerType.Organization);
EntityTypeConfiguration.HasOptional(d=>d.UserOwner)
.有许多(u=>u.Documents)
.HasForeignKey(d=>d.OwnerId)
。其中(d=>d.OwnerType==OwnerType.User);

在上下文上设置Linq查询时,我希望能够利用OrganizationOwner和UserOwner上的联接,这样就不必为每个文档对这些实体进行单独的选择。是否支持这种类型的关系,或者是否有更好的方法?谢谢。

您有没有找到解决方案?我们也有类似的问题。。。