-1

I have a problem with relationships in ASP.NET MVC. I have two models that have a one to many relationship. On the dependent class), the other class has two fields. I know that the dependent class applies the foreign attribute to establish a relationship. In my dependent class, am unsure if I should use to ForeignKey attributes to point to the other class. This is as per this tutorial on MVC site

Here is a simplified example of my problem.

public class Location {
    //Other fields here.
    public List<Link> Links{get; set;}
}

// dependent class
public class Link {
    //Other fields here...

    [ForeignKey("Location")]
    public string StartingLocation { get; set;}

    [ForeignKey("Location")]
    public string EndingLocation { get; set;}

    public Location Location  { get; set;}
}

Is what am doing on my dependent class legal and if so is it recommended?

Thanks and regards.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dev
  • 1,146
  • 2
  • 19
  • 30

1 Answers1

1

If your link entity is going to have 2 foreign keys pointing back to the same parent, you will need 2 navigational properties as well and then use the InverseProperty attribute to tell EF which belongs to which. https://msdn.microsoft.com/en-us/data/jj591583.aspx#Relationships

// dependent class
public class Link {
    //Other fields here...

     public string StartingLocation { get; set;}
     public string EndingLocation { get; set;}

     [InverseProperty("StartingLocation")]
     public Location StartLoc  { get; set;}

     [InverseProperty("EndingLocation")]
     public Location EndLoc  { get; set;}

}

This assumes StartingLocation and EndingLocation are foreign keys and you have used the fluent API to configure them as such since you are not following the "Id" convention.

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • Once you posted this and I went through the link which was so helpful. I also did some research and ran into [this](http://stackoverflow.com/questions/5716528/entity-framework-4-1-inverseproperty-attribute) SO post which was an eye opener. Thanks a lot. – Dev Jun 13 '15 at 16:51
  • One more question though, is it necessary to have the InverseProperty on both foreign keys on the two classes or can I just have them on one class pointing to the other?l – Dev Jun 13 '15 at 16:53
  • I've only needed to use it when I have 2 of the same foreign key in my model such as created by and updated by. You can (apparently) use it to define normal relationships, but if I have control over the design I prefer to just name my keys "Id" and my foreign keys "ParentId" and then EF does it's magic without any attributes or fluent configuration. – Steve Greene Jun 13 '15 at 17:05
  • I have run into a problem, am using the InverseAttribute on the navigation properties for both classes,so on each class they point back to each other. I get an error trying to **add migration** on PMC **Sequence contains more than one element** I don't run the update-database command so this isn't an issue as I have found on the web. I ran the project so it loaded on the browser a page that requested data from db so I could check if model backing had changed. I realized that on both classes, the InverseProperty was overlooked resulting in the error. I didn't use fluent API though. Kindly help – Dev Jun 13 '15 at 18:07
  • I found a way round. I commented both properties on both classes then added a migration without the InverseProperty properties. Then I uncommented all of them, added a new migration and updated db successfully. Thanks for your help. – Dev Jun 13 '15 at 18:31