none
TPT inheritance delete child casade to parent RRS feed

  • Question

  • HI,

    I've got a table activities. These hold the shared properties for all activities.

    through inheritance i've got two additional tabels -> ProjectActivities and QuestionActivities. These hold the specific properties for each type.

    With the EF i 'm trying to delete a ProjectActivity type. It neccesarry to delete the entry in the activities table as well.

    the data in the ProjectActivities table gets deleted but not the parentTable.

    Setting cascade delete wil only delete the child elements when deleting the parent but not the otherway arround.

    Is there a way (other then triggers) to enforce this businessrule?

    regards,

    Frans



    Monday, March 19, 2012 3:42 PM

All replies

  • I would have thought that just deleting a ProjectActivity row would have also deleted the Activity row, this would have been EF rather than SQL constraints job.  Will see if I can test today.

    Ben

    Tuesday, March 20, 2012 7:57 AM
  • Hi PietSwieb,

    Welcome to MSDN Forum.

    Are you using code-first, database first or model first?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 21, 2012 6:44 AM
    Moderator
  • Hi Allen,

    thanks for welcoming me.

    I've used the model-first appraoch.

    Best Regards,

    Wednesday, March 21, 2012 7:55 AM
  • Hi PietSwieb,

    I've tested in model first. After deleting the child record, the parent entity is deleted too. Below is my repro steps.

    1. Create model in designer.

    2. Generate Database from Model.

    3. write the code below to insert some records. Then, the database table parent has 2 records and child table has 1 record.

    using (var context = new TestContainer())
                {
                    parent p = new parent();
                    p.Name = "Allen";
                    child c = new child();
                    c.Name = "allen";
                    c.Description = "test";
                    context.parents.AddObject(p);
                    context.parents.AddObject(c);
                    context.SaveChanges();
                }

    3. Query out the child record and delete it.

    using (var context = new TestContainer())
                {
                    child c = (from t in context.parents.OfType<child>() select t).First();
                    context.parents.DeleteObject(c);
                    context.SaveChanges();
                }

    After above steps, I checked the database, and found the child table record has already deleted, as I expected, the relative record in parent table is also deleted.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 22, 2012 2:49 AM
    Moderator
  • Hi Allen,

    thanks for building the repo.

    i will investigate the differences between your repo and mine.

    I think it will be in the delete functionality, but i will get back to you on that. The parent is not made abstract is it?

    Best regards,

    Thursday, March 22, 2012 3:10 PM
  • Hi PietSwieb,

    Yes, the parent entity doesn't been set to abstract.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 23, 2012 2:44 AM
    Moderator
  • Hi PietSwieb,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 26, 2012 2:04 AM
    Moderator
  • HiAllen,

      I'm working.It is a fairlycomplex situation.

      TPTmanytableswhich relateto each other.

    loading works fine but thedelete is the hard part.

    some early conclusions;

    One mistake i found,setting the indication deleted before trying to delete the child elements. I Turned out,setting the delete sign will break up the lazy loading of the delete object even when it's not saved.

    Secondly the context of the elements where not the same. So deleting the child elements did not affect the parent context so it did not delete due to foreign key constraints.

    What I'm trying to find out now is why could i delete the child and not the parent.

    I thought that it would be an option, if the parent  should be deleted on deletion of the child.

    examplescenario: an user can also be an admin.

    so an user is the parenttype and the admin will be the child type.
    You can Promote an user to an admin but  you can not down grade to an user when the deletion of the admin record will force the deletion of the user role aswell.

    so i'm a kind of stuck there.

    i will keep you posted

    Best Regards



    Ithoughttheoptionwouldbetomaketheabstractparenttypesoi wouldbeanindication
    dit zou een indicatie kunnen zijn dat de parent row tegelijkertijd met de child verwijderd moest worden.

    Monday, March 26, 2012 6:59 PM
  • Hi PietSwieb,

    Based on the last post, I think the models seem different between us. Any update?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, March 28, 2012 1:54 AM
    Moderator