You would use a transaction scope...
using (TransactionScope scope = new TransactionScope())
{
var child= new Child();
child.Parent= parent;
DataContext.Parent.Add(parent);
DataContext.Entry(parent).State = EntityState.Modified;
DataContext.SaveChanges();
scope.Complete();
}
I cannot see you newing up your data context within your code snippet but I would of expected something more like this..
using (var context = new MyContext())
{
using (TransactionScope scope = new TransactionScope())
{
var child= new Child();
child.Parent= parent;
context.Parent.Add(parent);
context.Entry(parent).State = EntityState.Modified;
context.SaveChanges();
scope.Complete();
}
}
This way the context is disposed when you are done.
EDIT - As you want to update the parent instead of adding it.
You should retrieve the parent from the database as opposed to adding it and make the changes to the parent, in your code however there are no changes being made to the parent at all so of course it wont do an update on the parent record... you do not need to add the parent if it already exists.
using (var context = new MyContext())
{
using (TransactionScope scope = new TransactionScope())
{
var child= new Child();
var parentRecord = context.Parent.Find(parent.Id);
child.Parent= parentRecord;
context.SaveChanges();
scope.Complete();
}
}