none
Entity Framework Adding duplicate child objects to previously saved records RRS feed

  • Question

  • This is driving me crazy.  Sometimes when I save an object in entity framework then save another brand new object, it is duplicating some records in the first object I saved. 

    For example:

    I have a Organization that may have multiple purchase orders and each purchase order may have multiple bill of lading records.  I make a web service call to save a new purchase order with one bill of lading record. Everything saves correctly and this is purchase order # 55 and has one bill of lading record. 

    In the client I create another completely different new purchase order with a bill of lading record for the same organization and call the web service to save the record.  This is purchase order # 56 and everything looks good for PO 56 but when I go back and look at PO 55 it now has 2 Bill of lading records.  It seem the original bill of lading record that was added to PO 55 during the first web service call gets added again.  Why???

    When the the web service function is called with the second PO the po.organization_id property is set to the correct organization ID but the po.organization object is nothing (which is normal, I'm not changing organization so why send it).  I just set the foreign key to organization_id instead of populating object.  Once the ctx.Purchase_Order.ApplyChanges(PO) line is called the po.organization object is populated and po.organization object has 2 purchase orders, the current one being saved and the previous one that was saved.  I'm guessing it is pulling this out of the context even though I am creating a new context and this is causing the problem.

    Below is my WCF service function

    <PrincipalPermission(SecurityAction.Demand, Authenticated:=True)>
    Public Function SavePurchaseOrder(PO As Purchase_Order) As Purchase_Order 
        Try
            Using Tran As New System.Transactions.TransactionScope
                Try
    
                    Using ctx As New FracContext
                        ctx.Purchase_Order.ApplyChanges(PO)
                        ctx.SaveChanges()
                        PO.AcceptChanges()
    
                        For Each bl As Purchase_Order_BL In PO.Purchase_Order_BL
                            bl.AcceptChanges()
                        Next
    
                    End Using
    
                    Tran.Complete()
    
                Catch ex As Exception
                    Tran.Dispose()
                    Return Nothing
                End Try
            End Using
            Return PO
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    

    .  I may be doing something wrong.  Any help would be greatly appreciated!

    Wednesday, February 22, 2012 3:09 AM

Answers

  • I guess no one has an answer for this.  I'm going to write some stored procedures and save the data myself in this instance.  Kinda disappointing.

    Mike

    Friday, February 24, 2012 12:46 AM

All replies

  • try something to the effect of this, you need to save to your purchase order bl table then purchase order table be sure to dispose.

    for each bl as purchase_order_bl in po.purchase_order_bl

    ctx.Purchase_Order_BL.ApplyCurrentValues(bl)

    next

    ctx.SaveChanges()

    ctx.Purchase_Order.ApplyCurrentValues(PO)

    ctx.SaveChanges()

    ctx.Dispose()

    Wednesday, February 22, 2012 9:56 AM
  • That didn't work at all.  We have a very complex object graph and it keeps try to add extra object or change the key values on existing objects even though those objects have not been changed.  Could this be a bug in Entity Frameworks.   We are using version 4.0.30319.

    Mike

    Thursday, February 23, 2012 5:15 AM
  • I guess no one has an answer for this.  I'm going to write some stored procedures and save the data myself in this instance.  Kinda disappointing.

    Mike

    Friday, February 24, 2012 12:46 AM
  • Hi Mike,

    Would you please demo a small project to let me repro your question and ping here: v-alache@microsoft.com or share your models or Datatable structures with me, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 24, 2012 7:17 AM
    Moderator