【发布时间】:2011-02-23 04:04:23
【问题描述】:
我正在使用实体框架和使用 T4 模板生成的 POCO。我将生成的类放在一个单独的程序集中。
好的,一个非常简单的例子:
我在模型中有一个 Category 实体,它有 SubCategories(1-Many with SubCategory)。
当我使用以下代码时,我得到 ObjectContext 实例已被释放,不能再用于需要连接的操作。
Public Interface ICategoryRepository
Inherits IRepository(Of Category)
Function GetCategories() As IQueryable(Of Category)
Function GetCategoryByID(ByVal ID As Integer) As Category
End Interface
Public Class CategoryRepository
Implements ICategoryRepository
Public Function GetCategories() As System.Linq.IQueryable(Of Business.Category) Implements ICategoryRepository.GetCategories
Using db As New GTGContainer
Return db.Categories
End Using
End Function
Public Function GetCategoryByID(ByVal ID As Integer) As Business.Category Implements ICategoryRepository.GetCategoryByID
Using db As New GTGContainer
Return db.Categories.FirstOrDefault(Function(x) x.ID = ID)
End Using
End Function
End Class
Public Class HomeController
Inherits System.Web.Mvc.Controller
Private _CategoryRepository As GTG.Data.Repositories.ICategoryRepository
Public Sub New()
Me.New(New GTG.Data.Repositories.CategoryRepository)
End Sub
Public Sub New(ByVal Repository As GTG.Data.Repositories.ICategoryRepository)
_CategoryRepository = Repository
End Sub
Function Index() As ActionResult
Dim m As New HomeViewModel
m.Categories = _CategoryRepository.GetCategories
Return View(m)
End Function
End Class
Public Class HomeViewModel
Public Property Categories As List(Of GTG.Business.Category)
End Class
任何帮助都会很棒。谢谢!
【问题讨论】:
标签: asp.net-mvc vb.net entity-framework asp.net-mvc-3 repository-pattern