New Posts New Posts RSS Feed: One-to-many and one-to-one association between the same entities
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

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

 Post Reply Post Reply
Author
samir View Drop Down
Newbie
Newbie


Joined: 30-Mar-2011
Posts: 14
Post Options Post Options   Quote samir Quote  Post ReplyReply Direct Link To This Post Topic: One-to-many and one-to-one association between the same entities
    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?
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
samir View Drop Down
Newbie
Newbie


Joined: 30-Mar-2011
Posts: 14
Post Options Post Options   Quote samir Quote  Post ReplyReply Direct Link To This Post 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'


Edited by samir - 22-Jun-2011 at 6:04am
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
samir View Drop Down
Newbie
Newbie


Joined: 30-Mar-2011
Posts: 14
Post Options Post Options   Quote samir Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down