NPoco icon indicating copy to clipboard operation
NPoco copied to clipboard

Any way to do dynamic field updates on the server without raw sql?

Open grofit opened this issue 7 years ago • 2 comments

So I have a scenario where we have a stored procedure which only exists to increment a field, and I would love to remove it and just have it done via fluent/linq calls.

So for example I would want to do something like:

UPDATE TableName SET TableField = TableField + 1 WHERE SomeFilterField = someId

Problem is I cant find a way to do the TableField = TableField +1 without doing raw SQL as I am wanting to basically just trigger the update SQL side not from the model.

The problem originates because I only have the someId variable in scope so I dont have the current field value in a POCO, so currently the only way to do it seems to be doing:

  • Get the current object from the DB for someId
  • Increment the field on the POCO currentObject.TableField++;
  • Update the DB with the current object

This however requires 2 round trips and was hoping there would be a way to do this with 1 round trip without having to do it as raw SQL, so is there any support for this use case? If not no problem I appreciate it is niche and strays away from the POCO being the source of truth, but wanted to check.

grofit avatar Mar 06 '18 14:03 grofit

Yeh, there is no way to do this without custom sql at the moment.

schotime avatar Sep 29 '20 01:09 schotime

You could consider an extension message that creates the sql for you. Something like,

Db.IncrementColumn<Type>(“Column”)

Or Maybe using an action and some reflection.

Then, use the name of the property or attribute to generate your sql.

Shooting from the hip.

If you get it to work, maybe a PR :)

tbasallo avatar Oct 07 '20 02:10 tbasallo