c# - 即使使用正确的 `splitOn`,Dapper 也不会填充实体?

标签 c# sql-server dapper .net-4.6

查看这个简化的 SQL Server 查询:

set @userid2=...

SELECT *,
       SPLIT = '',
       userid2 = @userid2

    FROM   Comments c
           JOIN Users u
                ON  ...

我正在使用这个部分工作代码来执行 SP:

 await c.QueryAsync<Comment, User, SqlInt, CommentWithSenderAndUserId2>(@"insertCommentByImageId",
(message1, user, myint) => new CommentWithSenderAndUserId2 
                                {
                                  User = user,
                                  Comment = message1, 
                                  UserId2 = myint.MyIntValue
                                },
  new {imageid = imageId, userid1 = userid1, comment = comment}, //parms
  splitOn: "UserID,split",  //splits
  commandType: CommandType.StoredProcedure //command type
 )

如您所见,我返回了来自 CommentsUsers 的所有列 PLUS 一些 int 值.

好吧,我知道我不能像那样返回一个 int 值,我需要返回一个实体。

这就是我创建这个虚拟类来保存 int 值的原因:

public class SqlInt
    {
        public int MyIntValue { get; set; }
    }

如您所见,它是通用类型的一部分:

c.QueryAsync<Comment, User, SqlInt, CommentWithSenderAndUserId2>

所以基本上我采用了 CommentUserSqlInt 并将其全部放入 CommentWithSenderAndUserId2

那么问题出在哪里呢?

我的虚拟类的 int 值永远不会被填充,它总是 0(其他实体填充得很好)

enter image description here

I did read this post (就像我在 SP 中所做的那样)我应该添加一个分隔列,例如:

   SELECT *, 
           SPLIT = '',       <----------- Here
           userid2 = @userid2

问题

我做错了什么以及如何填充 myInt 值?

最佳答案

好吧,我发现了我的愚蠢错误。
问题是我创建了一个实体来保存 int 值,对吧?

  public class SqlInt
    {
        public int MyIntValue { get; set; }
    }

属性的名称是 MyIntValue。如果是这样,我为什么要这样做:

SELECT *,
           SPLIT = '',
           userid2 = @userid2

而不是正确的方法:

SELECT *,
           SPLIT = '',
           MyIntValue = @userid2 <---- change is here

现在我确实看到了正确的值(value)。

关于c# - 即使使用正确的 `splitOn`,Dapper 也不会填充实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42179621/

相关文章:

c# - ClientId 中的正则表达式匹配 ID

c# - 从嵌套用户控件(MVVM)导出数据

c# - 方法或操作未实现 : Reset all sessions in my application

sql-server - 有没有办法通过SQL查询先获取一些选定的数据,然后按升序获取其余数据

sql - "Order By"使用参数作为列名

c# - 与 SQL Server 一起使用时如何定期刷新 dapper.net 缓存

c# - 如何在 C# 代码隐藏中的动态文本框中设置输入类型 ="number"

sql - 使用 sql 选择的值作为另一个选择的行名

asp.net - 错误查询 : Incorrect syntax near ')'

c# - 使用 Dapper 映射 DBNull 时可以抛出异常吗?