Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

New Post: AddOrUpdate

$
0
0
I've recently encountered this problem where I need to add an entity or update (increment a property) if it already exists. This has to work with concurrency. Unfortunately there is no way to do this in a straightforward way in EF. I can try an insert, submit changes within a try/catch. If it failed, i need to query the entity first, update it then submit changes. Whew! That's a lot of code to do something very simple (and it actually won't work if I have multiple clients trying the same thing at once). I ended up executing the following SQL query directly: UPDATE xxx SET Value = Value + 1 WHERE yyy IF @@ROWCOUNT=0 INSERT INTO xxx VALUES (...); I'm not sure if this would be possible given how EF works but I'd like to do something like this: xxx.AddOrUpdate(new Entity(), e => e.Value++); Thoughts?

Viewing all articles
Browse latest Browse all 10318


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>