查看这个简化的 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
)
如您所见,我返回了来自 Comments
和 Users
的所有列 PLUS 一些 int
值.
好吧,我知道我不能像那样返回一个 int
值,我需要返回一个实体。
这就是我创建这个虚拟类来保存 int
值的原因:
public class SqlInt
{
public int MyIntValue { get; set; }
}
如您所见,它是通用类型的一部分:
c.QueryAsync<Comment, User, SqlInt, CommentWithSenderAndUserId2>
所以基本上我采用了 Comment
、User
、SqlInt
并将其全部放入 CommentWithSenderAndUserId2
那么问题出在哪里呢?
我的虚拟类的 int 值永远不会被填充,它总是 0
(其他实体填充得很好)
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/