Entity Framework 5.0 Code first with existing database. Reverse engineered the classes with power tools. Everything is working great. Database has two tables. One parent and one child with foreign key back to parent ID. ID's are all int with auto increment. I've added a number of parent records and now want to add child records to specific parents. The only way I can see to do this is find the appropriate parent ID by searching the Parents table for a name or some other property and return the ID. Then use that ID in the foreign key property when adding the child. I don't want to crate a new parent so is this the only way to add a child to an existing parent? All of the internet examples seem to add a child along with adding a new parent and not add a child to an existing parent. Any help would be greatly appreciated
-
To set a related entity you either set the navigation property to the parent entity or you set the foreign key. If you know the ID and you don't have the parent just the the foreign key. If you have the parent object just set the navigation property. If you don't know the ID nor have the parent object how do you know what parent the child entity is related to? – Pawel Oct 30 '12 at 23:07
-
Let me try with an example. Let's say the parent entity is teachers and I have Bob, Julie and Dave in the db. The child entity is classes with a one teacher to many classes relationship. I want to add a class to Julie but I don't have her primary ID because it was created automatically a few days ago when I entered her. So, must I search the teachers name property for "Julie" and return her primary ID and then use it as the new class foreign key? – user1626137 Oct 30 '12 at 23:23
-
The only way to identify Julie is to use the ID. The reason for this is that ID is always Unique why the name is not. You can bring the entity from the database using code like `context.Teachers.First(t => t.Name == "Julie") and use the ID or reference. There is no other way of setting relationship since you would not know what to set the relationship to. Also note that Julie might have been deleted in the meantime. – Pawel Oct 31 '12 at 02:02
1 Answers
"Julie" (Lerman) happens to be one of our teachers when it comes to EF. As she explains in her book DbContext there can be three properties involved in a parent-child association, two navigation properties (parent.Children, child.Parent) and a foreign key property (child.ParentId). You can set one of these or any combination of them in your code.
When EF is triggered to detect changes (e.g. when SaveChanges
is invoked) it starts a process called relationship fixup that ensures that the three properties will be in sync.
So you've got three options to add a child entity to a parent:
child.Parent = parentObject
. The parent object should be fetched from the database first.parentObject.Children.Add(child)
. Also requires a pre-existing parent object from the database.child.ParentId = parentId
, for which you only need to know the id. You can get the Id the way you suggested but just as well, for objects that you know will not be deleted, you can have it saved in some variable to reuse it across transactions.
With the first two options the child object does not need to be added to the context manually. EF knows enough to see that it's new. With the third option it does have to be added to context.Children
before SaveChanges
.

- 105,341
- 31
- 202
- 291
-
I've marked this as the answer and also point to Pawel's comment which both confirm my assumption that I need the parent entity or at least the parent ID before I can add a child. P.S. I have all of Julie's books and examples which have been a huge help while learning Entity Framework. – user1626137 Oct 31 '12 at 15:55