如何通过托管生命周期 ObjectContext 在 EF 中使用 POCO 实体加载导航属性?
我正在关注同一篇文章,如下管理实体框架 ObjectContext 生命周期。
我使用“POCO Entity Generator”工具从 EDMX 文件生成实体,并将所有实体放入类库中。我的模型(EDMX)位于单独的类库中。
我在数据访问层类库中引用了 Test.Entities.dll 和 Test.Model.dll。 在我的类库中,我有 UnitOfWorkScope 和 FacadeBase 类,它们负责管理我的 ObjectContext。所有其他类都继承自 FacadeBase 类。
Public Class WorkOrderDAL
Inherits FacadeBase(Of Test.Entities.WorkOrder)
这是 FacadeBase
Public MustInherit Class FacadeBase(Of T As System.Data.Objects.DataClasses.EntityObject)
Private _objectContextManager As ObjectContextManager(Of Test.Model.MyDataContext)
这是 Test.Entities.dll 中的实体类之一。
Partial Public Class WorkOrder
Inherits Global.System.Data.Objects.DataClasses.EntityObject
我的 DataAceessLayer 中有已编译的查询, 但它没有返回与该对象相关的导航属性。这是该示例。
ReadOnly qWorkOrderIDByWorkOrderID As Func(Of LAITEntities, Integer, IQueryable(Of WorkOrder)) = _
CompiledQuery.Compile(Of LAITEntities, Integer, IQueryable(Of WorkOrder))( _
Function(LAIT As LAITEntities, WorkOrderID As Integer) _
LAIT.WorkOrder.Include("Customer") _
.Include("Computer") _
.Include("Computer.Condition") _
.Include("Computer.Device") _
.Include("Computer.Brand") _
.Include("Computer.OS") _
.Where(Function(w) w.WorkOrderID = WorkOrderID))
Public Function SelectByWorkOrderID(ByVal WorkOrderID As Integer) As WorkOrder
Dim _result = qWorkOrderIDByWorkOrderID.Invoke(ObjectContext, WorkOrderID).AsEnumerable.SingleOrDefault
Return _result
End Function
I am following the same article as follows Managing Entity Framework ObjectContext lifespan.
I generate the Entities from my EDMX file by using "POCO Entity Generator" tool and put all the Entities in class library. I have my model (EDMX) in separate class library.
I referenced Test.Entities.dll and Test.Model.dll in my data access layer class library.
In my class library I have UnitOfWorkScope and FacadeBase classes that they take care of my ObjectContext. and all the other classes are inherited from FacadeBase class.
Public Class WorkOrderDAL
Inherits FacadeBase(Of Test.Entities.WorkOrder)
Here is the FacadeBase
Public MustInherit Class FacadeBase(Of T As System.Data.Objects.DataClasses.EntityObject)
Private _objectContextManager As ObjectContextManager(Of Test.Model.MyDataContext)
Here is one of the Entity classes in Test.Entities.dll.
Partial Public Class WorkOrder
Inherits Global.System.Data.Objects.DataClasses.EntityObject
I have the compiled query in my DataAceessLayer,
but It didn't return the navigation properties related to the object. Here is the sample of that.
ReadOnly qWorkOrderIDByWorkOrderID As Func(Of LAITEntities, Integer, IQueryable(Of WorkOrder)) = _
CompiledQuery.Compile(Of LAITEntities, Integer, IQueryable(Of WorkOrder))( _
Function(LAIT As LAITEntities, WorkOrderID As Integer) _
LAIT.WorkOrder.Include("Customer") _
.Include("Computer") _
.Include("Computer.Condition") _
.Include("Computer.Device") _
.Include("Computer.Brand") _
.Include("Computer.OS") _
.Where(Function(w) w.WorkOrderID = WorkOrderID))
Public Function SelectByWorkOrderID(ByVal WorkOrderID As Integer) As WorkOrder
Dim _result = qWorkOrderIDByWorkOrderID.Invoke(ObjectContext, WorkOrderID).AsEnumerable.SingleOrDefault
Return _result
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论