none
Insert that trigger insert question RRS feed

  • Question

  • I have generalized my question with a silly person example. Imagine that when I insert a person I want to insert a Parent object. A person can have a wide array of parents.

    I have a setup where I use my DataClassesDataContext class to overload the partial method:

    partial

     

    void InsertPerson(Person instance)

    and then inside I try to write:

    Parent p = new Parent {Name="Foo", ChildId=instance.Id};

    ExecuteDynamicInsert(p);

    ExecuteDynamicInsert(instance)

    that generates:

    The operation cannot be performed for the entity because it is not being change tracked.

    I also tried to add to instance like

    instance.Parents.Add(p)

    That also didn't work.

    I am really interested in what I'm not understanding. Ofcourse the solution would also be very nice.

    Thursday, July 8, 2010 12:25 PM

Answers

  • Hi Thomas,

     

    Two workarounds I can figure out:

     

    1.      Override the SubmitChanges() method and call GetChangeSet to get all the inserted Person instances, then we realize the logic to add the additional Parent instance. 

    ====================================================================================

            public override void SubmitChanges(ConflictMode failureMode)

            {

                var set = this.GetChangeSet();

                foreach (var i in set.Inserts)

                {

                    if (i is Person)

                    {

                        Parent p = new Parent() { Name = "Foo", ChildId = ((Person)i).id };

                        this.Parents.InsertOnSubmit(p);

                    }

                }

                base.SubmitChanges(failureMode);

    }

    ====================================================================================

     

    2.      Use our own logic like stored procedure to insert Person instance, so the stored procedure can add a new Parent as well. 

    Here are some references:

    http://msdn.microsoft.com/en-us/library/bb629289.aspx

    http://msdn.microsoft.com/en-us/library/bb882671.aspx

    http://msdn.microsoft.com/en-us/library/bb546188.aspx

     

     

    Please feel free to let me know if you have any questions.

     

    Have a nice weekend!

     

     

    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.
    Friday, July 9, 2010 2:13 AM
    Moderator

All replies

  • Hi Thomas,

     

    Two workarounds I can figure out:

     

    1.      Override the SubmitChanges() method and call GetChangeSet to get all the inserted Person instances, then we realize the logic to add the additional Parent instance. 

    ====================================================================================

            public override void SubmitChanges(ConflictMode failureMode)

            {

                var set = this.GetChangeSet();

                foreach (var i in set.Inserts)

                {

                    if (i is Person)

                    {

                        Parent p = new Parent() { Name = "Foo", ChildId = ((Person)i).id };

                        this.Parents.InsertOnSubmit(p);

                    }

                }

                base.SubmitChanges(failureMode);

    }

    ====================================================================================

     

    2.      Use our own logic like stored procedure to insert Person instance, so the stored procedure can add a new Parent as well. 

    Here are some references:

    http://msdn.microsoft.com/en-us/library/bb629289.aspx

    http://msdn.microsoft.com/en-us/library/bb882671.aspx

    http://msdn.microsoft.com/en-us/library/bb546188.aspx

     

     

    Please feel free to let me know if you have any questions.

     

    Have a nice weekend!

     

     

    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.
    Friday, July 9, 2010 2:13 AM
    Moderator
  • Hi Thomas,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice 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.
    Thursday, July 15, 2010 1:27 AM
    Moderator