【问题标题】:Dapper doesn't fill entity, even with the right `splitOn`?即使使用正确的“splitOn”,Dapper 也不会填充实体?
【发布时间】:2017-02-11 18:29:27
【问题描述】:

看看这个简化的 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(其他实体填充得很好)

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

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

问题

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    而不是正确的方法:

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

    现在我确实看到了正确的价值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多