This is a fairly fine point, and I expect the answer is "it's not a hot idea to begin with" - that said, it has a points that I'm interested in regardless, if someone is kind enough to indulge.
Model Code:
public partial class MyEntities : ObjectContext
{
// the idea is if the object is in a using block, this always gets called?
protected override void Dispose(bool disposing)
{
this.SaveChanges();
base.Dispose(disposing);
}
}
Client Code:
using(var model = new MyEntities())
{
// do something
// no worry about calling model.SaveChanges()
}
The issues I'm uncertain about are:
Is Dispose the right place to do this because I was thinking "Finalize" for some reason - I always get confused on C# destruction.
In the case an exception is thrown in the client code, ordinarily SaveChanges would be skipped and that's good, but if this works how I think, it'll always call it. Should I use try with an empty catch?
public partial class MyEntities : ObjectContext { protected override void Dispose(bool disposing) { try { this.SaveChanges(); } catch {} base.Dispose(disposing); } }