我正在构建一个简单的自定义模块,但在使用 DAL2 GetById 时遇到了问题。
这是我正在使用的表的 POCO 声明:
<TableName("KrisisStore_Products")> _
<PrimaryKey("ProductId", AutoIncrement:=True)> _
<Cacheable("Products", CacheItemPriority.Default, 20)> _
<Scope("PortalId")>
Public Class Product
Public Property ProductId As Int64
Public Property PortalId As Integer
Public Property ModuleId As Integer
''other columns here
End Class
我正在尝试使用以下方法从数据库中删除一条记录(为清楚起见,我删除了其他方法):
在模块视图中:
Dim pc As New ProductController
pc.DeleteItem(e.CommandArgument, PortalId)
这是我的产品控制器:
Imports System.Collections.Generic
Imports DotNetNuke.Data
Namespace Components
Public Class ProductController
Public Sub DeleteItem(ByVal itemId As Integer, ByVal PortalId As Integer)
Dim _item As Product = GetItem(itemId, PortalId)
DeleteItem(_item)
End Sub
Public Sub DeleteItem(ByVal p As Product)
Using ctx As IDataContext = DataContext.Instance()
Dim rep As IRepository(Of Product) = ctx.GetRepository(Of Product)()
rep.Delete(p)
End Using
End Sub
Public Function GetItem(ByVal itemId As Integer, ByVal PortalId As Integer) As Product
Dim p As Product
Using ctx As IDataContext = DataContext.Instance()
Dim rep As IRepository(Of Product) = ctx.GetRepository(Of Product)()
p = rep.GetById(Of Int32, Int32)(itemId, PortalId)
End Using
Return p
End Function
End Class
End Namespace
问题:
当代码到达 GetITem 函数中的以下行时
p = rep.GetById(Of Int32, Int32)(itemId, PortalId)
生成以下错误:
值不能为空。参数名称:来源
以下是堆栈跟踪的更多细节:
InnerException: Value cannot be null. Parameter name: source
FileName:
FileLineNumber: 0
FileColumnNumber: 0
Method: System.Linq.Enumerable.SingleOrDefault
StackTrace:
Message: DotNetNuke.Services.Exceptions.ModuleLoadException: Value cannot be null. Parameter name: source ---> System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at DotNetNuke.Data.RepositoryBase`1.GetById[TProperty,TScopeType](TProperty id, TScopeType scopeValue) at Krisis.Modules.KrisisStore.Components.ProductController.GetItem(Int32 itemId, Int32 PortalId) in C:\websites\dnndev.me\DesktopModules\KrisisStore\Components\ProductController.vb:line 51
问题
有人可以帮我弄清楚为什么会生成此 linq 错误,传递给函数的值是有效的,并且在提供 portalID 作为其范围时,其他存储库函数(如 GetItems)可以正常工作。