如何通过托管生命周期 ObjectContext 在 EF 中使用 POCO 实体加载导航属性?

发布于 2024-12-08 08:43:29 字数 1917 浏览 0 评论 0原文

我正在关注同一篇文章,如下管理实体框架 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文