【发布时间】:2011-06-17 02:03:17
【问题描述】:
在Entity中使用code first方法,是否可以建立绑定到单个外键的多个关系?
例如,假设我有以下课程:
public class BuzzItem
{
public int BuzzItemID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
// Collection of Comments posted about this BuzzItem (FOREIGN KEY)
public virtual ICollection<Comment> Comments { get; set; }
}
public class Merchant
{
public int MerchantID { get; set; }
public string Name { get; set; }
// Collection of Comments posted about this Merchant (FOREIGN KEY)
public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment
{
public int CommentID { get; set; }
public string Comment { get; set; }
// These would both be a FOREIGN KEY to their respectivate tables
public virtual BuzzItem BuzzItemID { get; set; }
public virtual Merchant UserID { get; set; }
}
是否可以将两个外键变量替换为一个可以建立双重关系并接受 BuzzItem 或 Merchant 对象作为关系的变量?
为了简洁起见,我从废品中起草了这个示例,因此如果我的代码中有任何拼写错误,我深表歉意,但希望我想要完成的总体思路很清楚。
【问题讨论】:
标签: database-design entity-framework-4.1 ef-code-first