2

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 they seem to be exactly what I need: I want to delete an object, once it is no longer referenced by its "parent" object.

I am using Visual Studio 2010 Premium to generate my database from an edmx file. As far as I understand, I need to include the foreign key of my "parent" object in the primary key of my "child" object table. However, I cannot find a way to tell Visual Studio to do this.

Can someone please help me out on this? Am I completely on a wrong path or am I just missing a setting somewhere?

Chris
  • 6,914
  • 5
  • 54
  • 80

2 Answers2

2

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

Chris
  • 6,914
  • 5
  • 54
  • 80
  • I do this and then get error 3002 "Problem in mapping fragments", because the primary key in the entity model no longer reflects the one in the DB. I had to remove the `ParentId` from the primary key, (and also delete mappings for the relationship, as that conflicts with the referential constraint) – Benjol Sep 11 '13 at 08:22
  • My solution is for model first so it assumes that you generate the database from the model. I don't know how it applies to existing databases. Does your solution still preserve the properties of identifying relationships? – Chris Sep 12 '13 at 00:26
  • Well, it looks like it does :) I can delete by simply doing a `parent.Remove(child)` and it doesn't complain when I `SaveChanges`... – Benjol Sep 12 '13 at 04:35
  • And did you check that the child is actually deleted from the database? Maybe you just have a relationship with a `0..1` on the parent end instead of a `1`? – Chris Sep 12 '13 at 05:29
  • Nope, it's 1 on the parent end, and the parentId is NOT NULL in the database, so that would shout at me. – Benjol Sep 12 '13 at 08:52
0

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.

Morteza Manavi
  • 33,026
  • 6
  • 100
  • 83
  • That only works when I delete the parent object. I cannot delete the child (no object context) and I must not delete the parent (still needed). All I can do is remove the relationship and when I do that, it doesn't delete the child. Cascade on delete only works when I actually delete the parent object; it does not delete the child, when I remove the relationship. Using your solution gives me an error message: "A relationship from the 'ParentChild' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'Child' must also in the 'Deleted' state." – Chris Sep 15 '10 at 07:06