1
我有一個名爲Discussion的EF4.1 CodeFirst模型實例。從EF CodeFirst模型實例中訪問DBContext
我想知道默認的「集合屬性訪問器」(例如下面的Discussion.messages.get)如何獲取管理當前對象的DB上下文,因爲我想編寫自定義訪問器,它們的工作方式相同。
基本上,我想知道如何實現this.GET_MANAGING_DB_CONTEXT()的後續:
public class Discussion {
// The default get; accessor seems to have access to the DBContext
public virtual ICollection<Message> messages { get; set; }
// How do I get access to the managing db context myself?
public ICollection<Monkey> monkeys {
get {
return new List<Monkey>(from m in this.GET_MANAGING_DB_CONTEXT().Monkeys select m where m.discussion == this.ID));
}
}
// Here's what I'm forced to do now, which requires passing DBContext around
// everywhere the object is used, breaking encapsulation of data storage
public ICollection<Monkey> monkeys(DBContext db) {
return new List<Monkey>(from m in db.Monkeys...);
}
/* ... */
}
比方說我想要做的代碼成員的任意過濾....基本上,我可以不像SQL那樣表達一整套複雜的約束,所以我想用C#來做一個SQL查詢,然後對結果做一堆處理。 – Seth