If I retrieve an object from the database using Linq to SQL in one method using an instance of DataContext, which closes on exit of that method, can I edit the object in a different method and with a different DataContext and have the changes take effect in the database?
i.e. Would something like the below work?
public void Foo()
{
using (var db = new DataContext())
{
Bar a = this.GetBar();
if (a != null)
{
a.Property1 = true;
db.SubmitChanges();
}
}
}
private Bar GetBar(string val)
{
using (var db = new DataContext())
{
return db.FirstOrDefault(x => x.Property2 == val);
}
}