Quickly in and quickly out (that's what Gandalf said in one of the movies... :) I think).
By that I mean that if some godly observer were sitting inside the CPU looking at your app,
what they should see is that your app runs the following sequence
{ connect, create context, make changes, commit them, destroy context, disconnect }
in a number of tens of milliseconds for (almost) each web request it receives.
Are you actually creating a context (entity container + unit of work)
that exists throughout the app globally ?
I would recommend a more classical and not-so-dbconnection-friendly approach (not to mention transactionally-sanitized):
using (var context = DataContextHelper.CreateContext()) {
var user = DataContextHelper.QueryAndSingleByLambdaBasedOnContext(
context,
u => u.Id == userId && u.Newsdesk.Id == newsdeskId
);
if (null == user)
// complain somehow (by exception or special return value)
user.Newsdesk = null;
// don't mark it anymore, in the straight-forward, good'ol way
// user will already have been marked
//DatabaseContext.Update(user);
context.SaveChanges();
}
It is generally bad (especially if you're not careful) to somehow host
a globally accessible context, that more than one thread can use concurrently.
I'm not talking about concurrency hazards while accessing some property.
I'm talking about the big picture
(maybe another request read the user and marked it as "not being modified" somewhere
between your Update
and the SaveChanges
).
Event if that's not the case, you shouldn't worry about concurrent connections to the db.
Concurrent access to the context is much harder to manage.