采用亚音速3 ActiveRecord的原子方式增加一个字段 [英] Atomically increment a field using SubSonic 3 ActiveRecord
问题描述
我特林增加使用亚音速3 ActiveRecord的一个MySQL数据库中的字段。在SQL中,这是我后:
更新人设messages_received = messages_received + 1,其中people_id = @ id_to;
我尝试以下,但它似乎没有工作(messages_received似乎永远是1):
_db.Update<人>()
。。设置(messages_received)EqualTo(X => x.messages_received == x.messages_received + 1)的
。凡(X => x.people_id == idTo)
。执行();
该做的工作:
字符串SQL =UPDATE人设messages_received = messages_received + 1,其中people_id = @ id_to;
变种Q =新SubSonic.Query.QueryCommand(SQL,_db.Provider);
q.AddParameter(id_to,idTo);
q.Provider.ExecuteQuery(q)的;
所以我有一个解决办法,但我只是想知道如果有可能做到这一点,而不诉诸普通的SQL?
答。作为参考,基于以下::罗布的建议
_db.Update<人>()
.SetEx pression(messages_received)。EqualTo(messages_received + 1)
。凡<人>(X => x.people_id == idTo)
。执行();
您可以使用旧的查询工具,这和使用SETEX pression:
db.Update(MyTable的)
.SetEx pression(messages_received + 1)
。凡(people_id)
.IsEqualTo(1)
。执行();
这是豪爽的 - 但希望你的想法:)
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();
You can use the old query tool for this and use "SetExpression":
db.Update("MyTable")
.SetExpression("messages_received +1")
.Where("people_id")
.IsEqualTo(1)
.Execute();
That's freehanded - but hopefully you get the idea :)
这篇关于采用亚音速3 ActiveRecord的原子方式增加一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!