在實體中使用代碼優先方法,是否可以建立綁定到單個外鍵的多個關係?實體框架4.1代碼優先 - 具有多重關係的外鍵?
例如,說我有以下類:
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或商人對象關係?
爲了簡潔起見,我從廢料中起草了這個例子,所以如果我在代碼中有任何拼寫錯誤,我很抱歉,但是我想要完成的總體想法很有希望。
它是否可以嘗試類似於:'public virtual int SourceID {get;組; }'然後使用RTTI將它投射到BuzzItem或Merchant對象?如果是這樣,你有沒有關於如何實現它的提示? – 2011-06-17 05:33:01