我经常使用该System.Data.Entity.DbExtensions
Include()
方法将复杂的实体字段包含在我的存储库的查询结果中。但是,当我将实体投影到新类中时,我似乎失去了包含的复杂实体字段的这种“具体化”。例如,假设我想Event
从我的仓库中返回一个对象,并且能够访问复杂的实体字段Assessment
:
public class EventRepository {
...
public IList<Event> GetEvents() {
using (var context = new MyDatabaseContext()) {
return context.Events
.Include(evnt => evnt.ActualAssessment)
.ToList();
}
}
...
}
然后我可以顺利运行以下代码,因为Include
我在上面使用了:
var repoEvents = new EventRepository();
var events = repoEvents.GetEvents();
Console.WriteLine(events[0].ActualAssessment.AssessmentDate.ToString());
但是假设我现在想将 s 投影到带有一些额外信息Event
的包装对象中,如下所示:ExtendedEvent
public class EventRepository {
...
public IList<ExtendedEvent> GetExtendedEvents() {
using (var context = new MyDatabaseContext()) {
return context.Events
.Include(evnt => evnt.ActualAssessment)
.Select(evnt => new {
TheEvent = evnt,
SomeExtraData = 123
})
.ToList()
.Select(evntInfo => {
return new ExtendedEvent {
TheEvent = evntInfo.TheEvent,
SomeExtraData = evntInfo.SomeExtraData
};
})
.ToList();
}
}
...
}
我现在尝试运行这段代码:
var repoEvents = new EventRepository();
var extendedEvents = repoEvents.GetExtendedEvents();
Console.WriteLine(extendedEvents[0].TheEvent.ActualAssessment.AssessmentDate.ToString());
这给了我错误“ObjectContext 实例已被释放,不能再用于需要连接的操作。” -ActualAssessment
尽管我使用了 ,但它并没有被预先加载Include
,显然是因为我将它投影到一个新的包装器对象中。我怎样才能使ActualAssessment
被包括在内?