1

我正在构建一个简单的自定义模块,但在使用 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)可以正常工作。

4

2 回答 2

3

在您的模型类中,您将 ProductId 定义为 Int64,但在您的控制器方法中,它作为 Integer 或 Int32 传递。您可能会认为这无关紧要,但我遇到过 PetaPoco 需要非常具体的实施才能正常工作的其他问题。也许这是一个问题?

于 2013-12-17T03:39:06.923 回答
0

乍一看,查询似乎带回了多条记录。是否有多个具有该 ID 的产品行?

于 2013-12-16T20:32:46.493 回答