5

I'm new to EF and struggling to implement the following scenario. I have an entity I'd like to have a navigation property to another of the same entity. E.g.

public class Stage {
    public int ID { get; set; }
    public int? NextStageID { get; set; }
    public string Name { get; set; }

    public virtual Stage NextStage { get; set;}
}

The only example I've found so far was where the entity had a parent / child relationship, i.e. the navigation property was an ICollection of the same entity. I tried adapting this but couldn't get it to work in my instance. Also, I only need it to be one way, i.e. the entity doesn't have a 'PreviousStage' property, just a 'NextStage' one. I'm configuring using Fluent API. Could someone advise if / how this can be achieved?

I am getting this error:

Unable to determine the principal end of an association between the types 'namespace.Stage' and 'namespace.Stage'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations

Edit Just realised in my slightly simplified example, I didn't show that NextStageID is optional (int?).

Kate
  • 1,556
  • 1
  • 16
  • 33
  • 2
    I think the relationship should already exist for the above model by convention. – galdin Dec 10 '15 at 17:16
  • @gldraphael I'm getting this error message: "Unable to determine the principal end of an association between the types 'namespace.Stage' and 'namespace.Stage'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations." – Kate Dec 10 '15 at 17:22
  • 1
    [Here's](http://stackoverflow.com/questions/19359608/ef-code-first-fluent-api-specifying-the-foreign-key-property) how to setup a FK with fluent configuration. – Jasen Dec 10 '15 at 17:43
  • Thanks @Jasen ! I had skipped over the .HasMany option as I assumed it would be for relationships with a many at one end or the other. Seems, as that post describes, it's also for relationships not involving a many. Bit odd, but that seems to have done the job! Do you want to post as an answer? – Kate Dec 10 '15 at 17:52

2 Answers2

9

You can explicitly define the relation as follows:

public class Stage {
    public int ID { get; set; }
    public int NextStageID { get; set; }
    public string Name { get; set; }

    [ForeignKey("NextStageID ")]
    public virtual Stage NextStage { get; set;}
}
user449689
  • 3,142
  • 4
  • 19
  • 37
  • Can you describe how to achieve the same using Fluent API? Just as I started that way and would prefer to keep it all in one place. – Kate Dec 10 '15 at 17:23
  • @Jasen provided the following link describing how to do this with Fluent API. Thanks both of you. http://stackoverflow.com/questions/19359608/ef-code-first-fluent-api-specifying-the-foreign-key-property – Kate Dec 14 '15 at 14:20
0

you need to add a parentId and Parent navigation property and Children navigation property so entity framework understands that is a recursive relation check the answer in this stack Overflow link

Community
  • 1
  • 1