使用 SubSonic 3 ActiveRecord 以原子方式递增字段
我正在尝试使用 SubSonic 3 ActiveRecord 增加 MySQL 数据库中的字段。在SQL中,这就是我所追求的:
UPDATE people SET messages_received=messages_received+1 WHERE people_id=@id_to;
我尝试了以下方法,但它似乎不起作用(messages_received似乎总是1):
_db.Update<person>()
.Set("messages_received").EqualTo(x => x.messages_received == x.messages_received + 1)
.Where(x => x.people_id == idTo)
.Execute();
这确实有效:
string sql="UPDATE people SET messages_received=messages_received+1 WHERE people_id=@id_to";
var q=new SubSonic.Query.QueryCommand(sql, _db.Provider);
q.AddParameter("id_to", idTo);
q.Provider.ExecuteQuery(q);
所以我有一个解决方案,但我只是想知道是否是否可以在不使用普通 SQL 的情况下完成此操作?
回答。作为参考,基于以下 Rob 的建议:
_db.Update<person>()
.SetExpression("messages_received").EqualTo("messages_received+1")
.Where<person>(x=>x.people_id==idTo)
.Execute();
I'm tring to increment a field in a MySQL database using SubSonic 3 ActiveRecord. In SQL, this is what I'm after:
UPDATE people SET messages_received=messages_received+1 WHERE people_id=@id_to;
I tried the following, but it didn't seem to work (messages_received always seemed to be 1):
_db.Update<person>()
.Set("messages_received").EqualTo(x => x.messages_received == x.messages_received + 1)
.Where(x => x.people_id == idTo)
.Execute();
This did work:
string sql="UPDATE people SET messages_received=messages_received+1 WHERE people_id=@id_to";
var q=new SubSonic.Query.QueryCommand(sql, _db.Provider);
q.AddParameter("id_to", idTo);
q.Provider.ExecuteQuery(q);
So I have a solution, but I'm just wondering if it's possible to do this without resorting to plain SQL?
Answer. For reference, based on Rob's suggestion below::
_db.Update<person>()
.SetExpression("messages_received").EqualTo("messages_received+1")
.Where<person>(x=>x.people_id==idTo)
.Execute();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此使用旧的查询工具并使用“SetExpression”:
这是徒手的 - 但希望您明白这个想法:)
You can use the old query tool for this and use "SetExpression":
That's freehanded - but hopefully you get the idea :)