SubSonic3 SetExpression problem
Next SubSonic3 query gives me an error:
Db.Update<Tag>()
.SetExpression("Popularity")
.EqualTo("Popularity+1")
.Where<Tag>(x => x.TagId == tagId)
.Execute();
Error: failed: System.FormatException : Failed to convert parameter value from a String to a Int32.
The sql that is generated is ok, but the collection of parameters contains two parameters that need to be set.
UPDATE [Tagging].[Tag]
SET Popularity=Popularity+1
WHERE [Tagging].[Tag].[TagId] = @0
开发者_JAVA技巧One of the parameters set @up_Popularity to 'Popularity+1'. Since this is the first parameter being set, sql triese to assign this string 'Popularity+1' to an integer.
Is this a bug or am I doing something wrong?
Db.Update<Tag>()
.SetExpression("Popularity = Popularity + 1")
.Where<Tag>(x => x.TagId == tagId)
.Execute();
This should work ... but I think it's for wholesale updates. Not sure. Your best bet is to use our CodingHorror:
new CodingHorror("UPDATE Tags SET Popularity = Popularity + 1 WHERE @1",
tagId).Execute();
I'd be amazed if that was supposed to work. When I need to have a string evaluated as part of the SQL (and not by SubSonic) I almost always end up having to use a CodingHorror.
However, you should be able to do this by using a separate query. Something like:
Db.Update<Tag>()
.Set("Popularity")
.EqualTo(Tag.SingleOrDefault(t => t.TagId == tagId).Popularity + 1)
.Where<Tag>(x => x.TagId == tagId)
.Execute();
精彩评论