none
Problems adding a Child entity with a Parent entity RRS feed

  • Question

  • We have two entities A and B. A is the parent and B is the child with an association defined.

    We are attempting to save a new entity B. This new entity B contains an A entity. Entity A already exists in the database. When we attempt to save the new entity B, L2S first attempts to save entity A as a new entity before saving entity B. Of course, this results in a duplicate key error. My question is this. Why is L2S attempting to save entity A when the entity already exists in the database? It should know this because the primary key has a value.

    How do I get around this?

    Saturday, October 31, 2009 12:11 AM

Answers

  • Hi Randy,

     

    Glad to see you again!

     

    If first query the DataContext and get an existing Parent Table object, then set the Child Table’s navigation property to the existing Parent Table, the Parent Table object won’t be marked as Added, but Unchanged, since the DataContext has traced the object.  We don’t need to call InsertOnSubmit on the Child Table because the DataContext has know it is in Added state.  For detail, please see the following code snippet:

    ==========================================================
                using (DataClasses1DataContext db = new DataClasses1DataContext())

                {

                    ChildTable c = new ChildTable()

                    {

                        id = 10,

                        Name = "New Child"

                    };

                    ParentTable p = db.ParentTables.First();

                    c.ParentTable = p;

                    db.SubmitChanges();

                }
    ==========================================================

     

    Another situation is that the Parent Table is not tracked by the DataContext.  We can new a Parent Table with the same primary key value which has existed in the database.  Or maybe the Parent Table object is passed in by another method.  In such a scenario, we can first attach the Parent Table to the DataContext and then add the Child Table object into the DataContext.  Please see the subsequent codes:

    ==========================================================
                using (DataClasses1DataContext db = new DataClasses1DataContext())

                {

                    ChildTable c = new ChildTable()

                    {

                        id = 10,

                        Name = "New Child"

                    };

                   

                    ParentTable p = new ParentTable()

                    {

                        id = 2,

                    };

     

                    c.ParentTable = p;

                    db.ParentTables.Attach(p, false);

                    db.ChildTables.InsertOnSubmit(c);

                    db.SubmitChanges();

                }
    ==========================================================

    In the above samples, the id columns are the PK of the Parent Table and Child Table. 

     

    If you have any problems, please feel free to let me know.  

     

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Randy Minder Wednesday, November 4, 2009 5:30 PM
    Monday, November 2, 2009 1:27 AM
    Moderator

All replies

  • Hi Randy,

     

    Glad to see you again!

     

    If first query the DataContext and get an existing Parent Table object, then set the Child Table’s navigation property to the existing Parent Table, the Parent Table object won’t be marked as Added, but Unchanged, since the DataContext has traced the object.  We don’t need to call InsertOnSubmit on the Child Table because the DataContext has know it is in Added state.  For detail, please see the following code snippet:

    ==========================================================
                using (DataClasses1DataContext db = new DataClasses1DataContext())

                {

                    ChildTable c = new ChildTable()

                    {

                        id = 10,

                        Name = "New Child"

                    };

                    ParentTable p = db.ParentTables.First();

                    c.ParentTable = p;

                    db.SubmitChanges();

                }
    ==========================================================

     

    Another situation is that the Parent Table is not tracked by the DataContext.  We can new a Parent Table with the same primary key value which has existed in the database.  Or maybe the Parent Table object is passed in by another method.  In such a scenario, we can first attach the Parent Table to the DataContext and then add the Child Table object into the DataContext.  Please see the subsequent codes:

    ==========================================================
                using (DataClasses1DataContext db = new DataClasses1DataContext())

                {

                    ChildTable c = new ChildTable()

                    {

                        id = 10,

                        Name = "New Child"

                    };

                   

                    ParentTable p = new ParentTable()

                    {

                        id = 2,

                    };

     

                    c.ParentTable = p;

                    db.ParentTables.Attach(p, false);

                    db.ChildTables.InsertOnSubmit(c);

                    db.SubmitChanges();

                }
    ==========================================================

    In the above samples, the id columns are the PK of the Parent Table and Child Table. 

     

    If you have any problems, please feel free to let me know.  

     

     

    Have a great day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Randy Minder Wednesday, November 4, 2009 5:30 PM
    Monday, November 2, 2009 1:27 AM
    Moderator
  • Thanks Lingzhi. That's what I was looking for.

    Randy
    Wednesday, November 4, 2009 5:30 PM