I'm trying to do something simple with locking in sql server 2008 (or directly from asp.net mvc if possible) and not really sure how to handle it.
I have a table (call it users), and username has to be unique. I want to do something along the lines of
var myuser = db.users.FirstOrDefault(u => u.username == username); if(myuser == null) { myuser = new user(username); db.users.add(myuser); } return myuser; I need that to be atomic so that no one can try to insert user foo after someone else did. If this can be implemented from asp.net mvc, that would be perfect, but if not, I'll need to do it directly in a stored procedure. Is locking the right way to do this in the stored procedure? If so, do I need to lock the whole users table? I've also seen something with the merge keyword when looking online, but I'm not sure if that will help.