【发布时间】:2012-12-10 20:19:04
【问题描述】:
我有一个名为 Discussion 的 EF4.1 CodeFirst 模型实例。
我想知道默认的“集合属性访问器”(例如下面的 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...);
}
/* ... */
}
【问题讨论】:
标签: entity-framework ef-code-first