【发布时间】:2012-12-17 13:32:07
【问题描述】:
在 EF5 中使用 DBContext - 根据日期范围等条件进行过滤和部分加载之后。
我正在尝试生成一个完整的图形或对象树 - Persons->Events where the only Events that are included are within a date range. 所有这些同时保留通过以下方式获得的标准更改跟踪:
Dim Repository As Models.personRepository = New Models.personRepository
Private Sub LoadData()
Dim personViewModelViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("personViewModelViewSource"), System.Windows.Data.CollectionViewSource)
Repository.endDate = EndDate.SelectedDate
Repository.startDate = StartDate.SelectedDate
personViewModelViewSource.Source = Repository.collectionOfpersons
End Sub
列表框和数据网格都绑定为适当的数据源。 POCO 模板已修改为将 INotifyProperty 事件放入导航属性类 Events 中。
我已经为此奋斗了好几天,并且无论是在延迟加载还是显式加载上进行过滤都不起作用。 在大量阅读博客/章节之后,我意识到与 Include 相关的相当不真实的限制; 相反,我正在尝试显式加载。顺便说一句,我正在使用 DBContext 书。
无法从数据库中只取回部分事件数据是 100% 的交易破坏者,因为每个人可能有数十万个事件。 对于我或我的老板来说,Entity Framework 没有使这个功能相当明显是没有意义的——我们是否遗漏了什么?
我在注释代码中留下了尝试说明我尝试过的一些路径。类本身就是该方法所属的存储库。我将进一步编辑这个问题,以澄清我尝试了多少条路线,因为它已经很多了。 View 使用存储库层和 ViewModel,因此 XAML 背后的代码非常少。
提前寻求帮助,谢谢!
Public Overridable ReadOnly Property AllFiltered(startdate As Date, enddate As Date) As ObservableCollection(Of person) Implements IpersonRepository.AllFiltered
Get
Dim uow = New UnitOfWork
context = uow.Context
Dim personQuery = context.persons.Include(Function(p) p.events).AsQueryable.Where(Function(x) x.personID = 10).FirstOrDefault
'Dim eventQuery = From e In context.Notes
' Where e.eventDateTime >= startdate And e.eventDateTime <= enddate
' Select e
'Dim personQuery As person = From r In context.persons
' From e In eventQuery
' Where r.personID = e.personID
' Select r, e
Dim singleperson = personQuery
'For Each r As person In personQuery
' persons.Add(r)
'Next
' context.Entry(eventQuery).Collection()
' context.Entry(personQuery).Reference(personQuery).Load()
context.Entry(singleperson).Collection(Function(d) d.events).Query().Where(Function(x) x.eventDateTime > startdate And x.eventDateTime < enddate).Load()
Return context.persons.Local
End Get
End Property
注意:我通过 PostSharp 使用日志记录/异常处理,而不是污染代码。
以下是我使用以前的路径产生的一些错误。
实体类型 DbQuery`1 不是当前上下文模型的一部分。
包含路径表达式必须引用在类型上定义的导航属性。对引用导航属性使用虚线路径,对集合导航属性使用 Select 运算符。
参数名称:路径
无法转换类型为“System.Data.Entity.Infrastructure.DbQuery1[VB$AnonymousType_02”的对象
[Entity.Person,Entity.Notes]]' 输入'System.Collections.ObjectModel.ObservableCollection`1[Entity.Person]'。
更新:我尝试过的另一条路线仍然无法飞行:
Private Property _collectionOfPersons As ObservableCollection(Of Person)
Public ReadOnly Property collectionOfPersons As ObservableCollection(Of Person)
Get
For Each Person In context.Persons
_collectionOfPersons.Add(ReturnSinglePerson(startDate, endDate, Person.PersonID))
Next
Return _collectionOfPersons.Where(Function(x) x.events.Where(Function(e) e.eventDateTime > startDate And e.eventDateTime < endDate))
End Get
End Property
Public Overridable ReadOnly Property SinglePerson(startdate As Date, enddate As Date) As ObservableCollection(Of Person) Implements IPersonRepository.AllFiltered
Get
Dim PersonQuery = context.Persons.Include(Function(p) p.events).AsQueryable.Where(Function(x) x.PersonID = 10).Select(Function(x) x).FirstOrDefault
Dim Person = PersonQuery
context.Entry(Person).Collection(Function(d) d.events).Query().Where(Function(x) x.eventDateTime > startdate And x.eventDateTime < enddate).Load()
Return context.Persons.Local
End Get
End Property
Public Function ReturnSinglePerson(startdate As Date, enddate As Date, id As Integer)
Dim PersonQuery = context.Persons.Include(Function(p) p.events).AsQueryable.Where(Function(x) x.PersonID = id).Select(Function(x) x).FirstOrDefault
Dim Person = PersonQuery
Return Person
End Function
另一个镜头: 公共可重写只读属性 FilteredPersons(startdate As Date, enddate As Date) As ObservableCollection(Of Person) 实现 IPersonRepository.AllFiltered 得到 context.Persons.Load() Dim DateCriteria = Function(e) e.events.Where(Function(d) d.eventDateTime > startdate And d.eventDateTime
Dim Res = New ObservableCollection(Of Person)
For Each Person In context.Persons.Local.Select(Function(x) x).Where(DateCriteria)
Res.Add(Person)
Next
Return Res
End Get
End Property
给予:
未找到类型“ObservableCollection(Of DailyNotes)”上的公共成员“Where”。
非常接近,只有我在列表框中得到了很多重复的名称 - 但导航通过并且日期标准有效。
<ExceptionAspect>
Public Overridable ReadOnly Property FilteredPersons(startdate As Date, enddate As Date) As ObservableCollection(Of Person) Implements IPersonRepository.AllFiltered
Get
context.Persons.Load()
Dim test = From r In context.Persons
From e In context.Notes
Where e.eventDateTime > startdate And e.eventDateTime < enddate
Join rr In context.Persons On e.PersonID Equals rr.PersonID
Select r, e
Dim Res = New ObservableCollection(Of Person)
For Each Person In test
Res.Add(Person.r)
Next
Return Res
End Get
End Property
不要尝试这个:)。它只选择子属性。
Public ReadOnly Property collectionOfResidents As ObservableCollection(Of resident)
Get
For Each resident In context.residents
_collectionOfResidents.Add(ReturnSingleResident(startDate, endDate, resident.residentID))
Next
Return _collectionOfResidents.Select(Function(x) x.events.Where(Function(e) e.eventDateTime > startDate And e.eventDateTime < endDate))
End Get
End Property
我希望在这个问题中添加我的其他尝试可以提示其他答案并帮助其他人在第一次解决这个问题时看到他们可以进入的圈子!
【问题讨论】:
标签: entity-framework entity criteria dbcontext navigation-properties