我关注的文章如下Managing Entity Framework ObjectContext lifespan。
我使用“POCO实体生成器”工具从我的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中的一个Entity类。
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