Reputation: 1972
I have several updates to database tables that may or may not happen. Rather than calling submitchanges() multiple times I would rather call once.
Potentially there will be no change to submit to the database.
My question is whether Linq actually does anything in this scenario? or whether it is smart enough to know that no update is required and nothing happens - hence no overhead.
Thanks for your advice.
if (setTempResult)
{
tr = new tempResult();
db.tempResults.InsertOnSubmit(tr);
tr.userId = tl.us.userId;
tr.result = serializer.Serialize(tl.responseList);
tr.resultTime = DateTime.Now;
}
if (string.IsNullOrEmpty(tl.actionCategory))
{
action ac = new action();
ac.userId = tl.us.userId;
ac.operation = "STR";
db.actions.InsertOnSubmit(ac);
}
db.SubmitChanges();
Upvotes: 1
Views: 495
Reputation: 41403
After looking at the code for Linq-to-SQL on .NET 4.5, it appears that it depends on whether your code is in a transaction or not. The pseudo-code is:
public void SubmitChanges()
{
if (inTransaction)
{
try
{
// Verify the connection is open, if it is the "clear" it
// Begin a transaction
// Send changes, if any
// Commit transaction
}
catch
{
// Rollback transaction
}
finally
{
// Close the connection, if it was opened above
}
}
else
{
// Send changes, if any
}
}
As you can see, there is going to be the overhead of opening/closing/clearing the connection and beginning/committing a transaction, if it's not already wrapped in an explicit transaction.
Upvotes: 2
Reputation: 104
Depending on your backend, it should be able to detect whether changes have been made. For instance Entity Framework uses so called Object States in its DbContext and the attached Entities.
You can read about this here: http://msdn.microsoft.com/en-us/library/bb386982(v=vs.110).aspx
Upvotes: 1