【发布时间】:2015-04-24 06:08:51
【问题描述】:
我在将外键设置为引用三个可能实体之一的实体时遇到问题。类如下:
public class Target
{
public int Id { get; set; }
public int? AttachmentId { get; set; }
public virtual Attachment Attachment { get; set; }
}
public class SubTarget
{
public int Id { get; set; }
public int? AttachmentId { get; set; }
public virtual Attachment Attachment { get; set; }
}
public class Procedure
{
public int Id { get; set; }
public int? AttachmentId { get; set; }
public virtual Attachment Attachment { get; set; }
}
public class Attachment
{
public int Id { get; set; }
public int? TargetId { get; set; }
public int? SubTargetId { get; set; }
public int? ProcedureId { get; set; }
public virtual Target Target { get; set; }
public virtual SubTarget SubTarget { get; set; }
public virtual Procedure Procedure { get; set; }
}
因此,三个第一类中的每一个都可能有也可能没有Attachment,每个Attachment 可以而且必须引用三个第一类中的一个。
首先:是有可能有这样的外键设置吗?
其次:我如何通过代码优先迁移来完成它?
在当前设置下,我收到一条错误消息,指出“无法确定外键关系的主体端”,并且通过注释掉 Attachment 类中的虚拟属性,关系似乎倒退了,即。 Target 有一个引用 Attachment 的外键。
【问题讨论】:
标签: c# entity-framework ef-code-first entity-framework-6