I am using L2S to check if a key already exists, and if it doesnt, create and insert one. This same DataContext is also used to execute other queries within the same C# method. I am implimenting the using keyword to dispose the DataContext after I am done with it. I hooked up sql profiler and saw that it would Login, execute the query, then logout. It seems to do this even if the same DataContext will perform a query or update later. I thought the datacontext only logged in once, and performed all the queries during that one session? Does how often you call SubmitChanges() matter? Or what am i missing?
mock of what i am seeing:
using(Datacontext)
{
//Audit Login
var b = DataContext.Table.FirstOrDefault(t=>t.Id == 4);
//RPC: Completed
//Audit Logout
//Audit Login
var x = DataContext.OtherTable.Any(t=>t.Id == 4);
//RPC: Completed
//Audit Logout
}
Update
sql profiler is showing the ‘duration’ of these logouts as a couple hundred milliseconds. Thats time my app is waiting right?
FirstOrDefaultexecutes a query since it needs to give you a result back, not a potential collection – same goes forAny. When you create a DataContext it doesn’t log in right away. I can assure you, though this may look “bad”, it’s completely fine.