0

Hi I have a code specified below, it is currently Inserting the new rows into child table and updating the parent table.

var child= new Child();
child.Parent= parent;
DataContext.Parent.Add(parent); 
DataContext.Entry(parent).State = EntityState.Modified;

Now I want both the parent and child tables to be updated in a single transaction. It will be very helpful if any one could help me.

Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
swathi
  • 119
  • 2
  • 11
  • 1
    This question is answered here: http://stackoverflow.com/questions/815586/entity-framework-using-transactions-or-savechangesfalse-and-acceptallchanges – ChristofSenn Jan 23 '14 at 11:04
  • After executing the above 4 lines am also executing the following line : DataContext.SaveChanges(); my question is how to update both the tables. – swathi Jan 23 '14 at 11:31

1 Answers1

0

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();
    }
}
BenjaminPaul
  • 2,931
  • 19
  • 18
  • Is it like if that 'using context' is not applied the child data will be inserted instead of getting updated? With my code am able to insert child details and update parent details. But what I wanted is to update both the tables. – swathi Jan 23 '14 at 12:07
  • Edited my answer... I have no idea what the code is like before the snippet you added but if you are retrieving the parent are you actually making a change to it? You should not need to 'add' the parent again if it already exists and from the code you have shown, you are not actually changing anything on it so why would EF do an update? There is nothing to update! – BenjaminPaul Jan 23 '14 at 12:16
  • Actually with my code I am able to update the parent table with the following code :DataContext.Entry(parent).State = EntityState.Modified; DataContext.SaveChanges(); since am giving the entity state as modified, it is updating the data. I donot have any problem in updating parent table, the issue is only with child table. I want to update it but it is getting inserted everytime with the above code. – swathi Jan 23 '14 at 12:53
  • You are clearly adding a new child? How would Entity Framework update a child when it does not exist? ... Im sorry but your question is not clear enough and I am unsure as to what exactly you are attempting. If you new a child object and then add it to the parent of course it adds a new child record... – BenjaminPaul Jan 23 '14 at 13:39
  • Ya exactly am doing the same way that you mentioned. Now I am adding it, I dont know to how to update the child details. I want to do something like this which you had mentioned for parent : var parentRecord = context.Parent.Find(parent.Id); to do this my child table will be having multiple records for single parent. I am really sorry if I am irritating, am very new to this EF so only am asking so many doubts. – swathi Jan 23 '14 at 14:23