NPoco
NPoco copied to clipboard
Any way to do dynamic field updates on the server without raw sql?
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.
Yeh, there is no way to do this without custom sql at the moment.
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 :)