是否可以建立基于实体框架中的条件的关系?我的模型看起来像这样......
public class Document
{
public int Id { get; set; }
public string Name { get; set; }
public OwnerType OwnerType { get; set; }
public int OwnerId { get; set; }
public virtual Organization OrganizationOwner { get; set; }
public virtual User UserOwner { get; set; }
}
public enum OwnerType
{
Organization = 1,
User = 2
}
public class Organization
{
public int Id { get; set; }
public string Name { get; set; }
//[other properties specific to Organization]
public virtual List<Documents> Documents { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
//[other properties specific to User]
public virtual List<Documents> Documents { get; set; }
}
所以,我想要建立一个关系,以便在OwnerType == OwnerType.Organization时自动填充Document实例的OrganizationOwner属性,并在OwnerType == OwnerType.User时填充UserOwner属性
这是否可以在EntityFramework中建立这种关系 - Code First?在映射中有这样的东西......
EntityTypeConfiguration<Document>.HasOptional(d => d.OrganizationOwner)
.WithMany(o => o.Documents)
.HasForeignKey(d => d.OwnerId)
.Where(d => d.OwnerType == OwnerType.Organization);
EntityTypeConfiguration<Document>.HasOptional(d => d.UserOwner)
.WithMany(u => u.Documents)
.HasForeignKey(d => d.OwnerId)
.Where(d => d.OwnerType == OwnerType.User);
我希望能够在上下文中设置Linq查询时利用OrganizationOwner和UserOwner上的连接,这样我就不必为每个Document对这些实体进行单独选择。这种关系是否受到支持,或者有更好的方法吗?感谢。