Petpoco问题与可空SQl服务器smallint和int16在vb.net

我有一个SQL Server表与视图和smallint字段。 由于您无法在视图中指定它,视图中的所有数据都会自动标记为可从Petapoco T4生成器中取消。 到目前为止,我还没有遇到所有其他数据类型(guid,int,tinyint,string等),但似乎可空的smallint会导致一些问题。

VB.NET中的T4生成器为SMALLINT字段创建了以下代码:

    Private mPasswordResetDays As Integer?
    <Column> _
    Public Property PasswordResetDays() As Integer?
        Get
            Return mPasswordResetDays
        End Get
        Set
            mPasswordResetDays = Value
        End Set
    End Property

BUt我收到一个异常:

从'System.Int16'投射到'System.Nullable`1 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]'

在Petapoco线2677:

Line 2675:       else
Line 2676:       {
Line 2677:          converter = src => Convert.ChangeType(src, dstType, null);
Line 2678:       }
Line 2679:            }

从PetaPoco执行的SQL命令非常简单:

SQL Command: SELECT UserId, AllowPersistentCookie, MinPasswordLenght,
NonAlphanumericPasswordChars, AlphanumericPassword, PasswordResetDays FROM
EVA_vw_UserLoginStatusFromRoles
WHERE UserId = @0 -> @0 [String] = "d85674ab-d389-4548-ad89-5322892ca0e2"

我认为在ExecuteScalar命令返回null时注意到了一个类似的问题,但是这一行是不同的,代码也是这样。

如果我从Integer更改类型? 整数,使他们不能为空,一切工作正常。

有关Petapoco和SQL Server Smallints的这个问题的任何提示?

附录:查看代码是:

SELECT     a.UserId, CAST(MIN(CAST(b.AllowPersistentCookie AS int)) AS bit) AS AllowPersistentCookie, MAX(b.MinPasswordLenght) AS MinPasswordLenght, 
                  CAST(MAX(CAST(b.NonAlphanumericPasswordChars AS int)) AS bit) AS NonAlphanumericPasswordChars, CAST(MAX(CAST(b.AlphanumericPassword AS int)) AS bit) 
                  AS AlphanumericPassword, MIN(b.PasswordResetDays) AS PasswordResetDays
FROM         dbo.EVA_UsersInRoles AS a INNER JOIN
                  dbo.EVA_Roles AS b ON a.RoleId = b.RoleId
WHERE       (DATEDIFF(day, CONVERT(date, GETUTCDATE()), a.StartDate) <= 0) AND (DATEDIFF(day, CONVERT(date, GETUTCDATE()), a.EndDate) >= 0) OR
                  (DATEDIFF(day, CONVERT(date, GETUTCDATE()), a.StartDate) <= 0) AND (a.EndDate IS NULL) OR
                  (DATEDIFF(day, CONVERT(date, GETUTCDATE()), a.EndDate) >= 0) AND (a.StartDate IS NULL) OR
                  (a.EndDate IS NULL) AND (a.StartDate IS NULL)
GROUP BY a.UserId

此问题以及解决方案请访问https://github.com/toptensoftware/PetaPoco/issues/153

PetaPoco需要补丁:

- converter = delegate(object src) { return Convert.ChangeType(src, dstType, null); };
+ var underlyingType = Nullable.GetUnderlyingType(dstType) ?? dstType;
+ converter = src => Convert.ChangeType(src, underlyingType, null);

意见是红鲱鱼; 你可以通过返回一个int NULL列作为uint?在具体表格上重现这一点uint? 或类似的。


它接近你使用表生成代码,但现在你正在从视图中获取值。 SQL必须暗示列不可视为视图。

链接地址: http://www.djcxy.com/p/42825.html

上一篇: Petapoco issue with nullable SQl Server smallint and int16 in vb.net

下一篇: Make A Date Null