Implementing identifying relationships with EF4

前端 未结 2 745
执念已碎
执念已碎 2021-01-15 17:17

I\'m currently in a situation where I need to delete entities without having access to the associated ObjectContext. I read about identifying relationships and

相关标签:
2条回答
  • 2021-01-15 18:01

    I finally figured it out:

    Go to your Child entity and create a scalar property ParentId. Set this property as entity key (making it a primary key, together with your Id property of your Child entity). Next go to your ParentChild relationship and add a referential constraint. Principal for the constraint is your Parent and Dependant is your Child. Dependant property must be the property you just created on your Child (i.e. ParentId). Save everything and you're good to go.

    Basically this is described as "scenario 2" in this blog post: http://mocella.blogspot.com/2010/01/entity-framework-v4-object-graph.html

    0 讨论(0)
  • 2021-01-15 18:11

    No, you are in the right path. What you need to do is in the EDM designer, after creating your 2 entities (Parent and Child), right click on the Parent Entity and select Add => Association... and then specify Multiplicity and Navigation property names, and click Ok. You'll see that VS create an association in between which will result on a relationship between these 2 table later on when you generate a database from your model.
    Do not create a property like ParentID on your Child entity as it will be automatically created by the designer once you create the association.
    Furthermore, you can right click on the association in the EDM designer and Select Properties and Select "Cascade" on "End2 OnDelete" option so that the child will be deleted when the parent is deleted.

    0 讨论(0)
提交回复
热议问题