Print Page | Close Window

One-to-many and one-to-one association between the same entities

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2787
Printed Date: 14-May-2026 at 1:38am


Topic: One-to-many and one-to-one association between the same entities
Posted By: samir
Subject: One-to-many and one-to-one association between the same entities
Date Posted: 21-Jun-2011 at 3:07pm
Hello.
I´m having a hard time trying to accomplish the following scenario:
 
There are two entities: Father and Child.
Father has a 1 to many association to Child.
 
I´d like to have a navigation field in Father called PreferredChild which would point to a specific Child. How can I do this with DevForce / Entity Framework?



Replies:
Posted By: sbelini
Date Posted: 21-Jun-2011 at 3:33pm
Hi Samir,
 
If you take a look at the NorthwindIB sample database, you will see that the Employee entity, has this same pattern. (i.e. 1..* and 1..1)
 
If the above is not enough, what exactly is the issue you are coming across?
 
Regards,
   Silvio.


Posted By: samir
Date Posted: 22-Jun-2011 at 6:03am
Hi Silvio,
 
I think my post´s subject was misleading. The example you provided has a ReportsToEmployeeId that refers to the Id of the same table, however what I want is an hypothetical Father table to have a 1..* relation to a Child table while also having a 1..1 relation to the same Child table.
 
Here´s the screenshot of what I´d like:
 
If I try to build this DevForce throws this:
 
Error 2 #error: 'DevForce requires that foreign key columns be included in the model. This must be set when the EDMX is first created. If you are upgrading from DevForce 2009, you cannot use the old model and should recreate it using the Entity Framework for .NET 4.0.  The following associations were not set up with foreign key associations enabled: FatherChild1'


Posted By: sbelini
Date Posted: 23-Jun-2011 at 1:36pm
Hi Samir,
Thanks for clarifying.
The the error you are getting:
In the 1..1 relation properties add the Referential Constraint. You will need to create a FavoriteChildID foreign key in the Parent entity.
The next problem you would face is that a 1..1 relation will complain if either of the linking FKs are not PKs.
For this problem, I suggest that instead of using a 1..1 relation, you use a Child 1..* Parent relation instead. The additional step would be removing the 'Fathers' Navigation Property from the Child entity.
Regards,
Silvio.


Posted By: samir
Date Posted: 24-Jun-2011 at 2:50pm
Hi Silvio,
 
Thanks for your suggestion, it works!
The problem I´m facing now is that I can´t save both a Father and his favorite Child in the same operation as I get a "Unable to determine a valid ordering for dependent operations" - which is understandable. Is there any work-around on the server side for this? I really wouldn´t like to first save the Father and then (after Father has its final Id) save it´s favorite Child.
 
Thanks,
Samir



Print Page | Close Window