帮助使用存储库模式吗?
我正在使用实体框架以及使用 T4 模板生成的 POCO。我将生成的类放在单独的程序集中。
好的,这是一个非常简单的例子:
我在模型中有一个类别实体,它有子类别(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
任何帮助都会很棒。谢谢!
I am using the Entity Framework with POCO's generated using the T4 Templates. I have the generated classes in a separate assembly.
Ok, so a very simple example:
I have a Category entity in the model which has SubCategories (1-Many with SubCategory).
When I use the following code, I get The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
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
Any help would be great. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您的 GetCategories() 返回一个 IQueryable,它实际上不是内存中的集合。当您的视图尝试枚举它时,它会尝试访问数据库,但它显然无法做到这一点。简单的补救措施是通过调用 GetCategories() 中的 ToList() 将其转换为内存中集合。即 db.Categories.ToList()。
This is because your GetCategories() returns an IQueryable, which is NOT actually an in-memory collection. When your View tries to enumerate it, it attempts to hit the database, which it obviously cannot do. The easy remedy is to turn it into an in-memory collection by calling ToList() in GetCategories(). That is, db.Categories.ToList().
让上下文贯穿存储库的生命周期。
Let the context live through the life of repository.