2017-02-11 77 views
2

望着這簡化 SQL Server查詢:即使使用正確的`splitOn`,Dapper不會填充實體嗎?

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 
) 

正如你可以看到我returnning所有列均來自CommentsUsersPLUS一些int值。

那麼我知道我不能像這樣返回一個int的值,我需要返回一個實體。

這就是爲什麼我創建了這個虛擬類,它保持一個int值:

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

正如你所看到的,它是通用類型的一部分:

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

所以基本上我'正在拍一張Comment,User,SqlInt並全部放入CommentWithSenderAndUserId2

所以哪裏有問題 ?

我啞類的int值永遠不會被填補,它總是0(其它實體填寫就好)

enter image description here

I did read this post(我在SP所做的那樣),我應該添加一個分離塔如:

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

問題

我在做什麼錯誤,我怎樣才能得到myInt值?

回答

1

嗯,我發現我的愚蠢的錯誤。
問題是我創建了一個實體來保存int值,對吧?

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

該物業的名稱是MyIntValue。如果是這樣,爲什麼我這樣做:

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

,而不是正確的做法:

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

現在我看到正確的價值。

相關問題