Atomically increment a field using SubSonic 3 ActiveRecord.

Posted by cantabilesoftware on Stack Overflow See other posts from Stack Overflow or by cantabilesoftware
Published on 2010-03-11T23:19:42Z Indexed on 2010/03/12 4:07 UTC
Read the original article Hit count: 496

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?

© Stack Overflow or respective owner

Related posts about subsonic3

Related posts about activerecord