Reputation: 204
I have an entity with below properties:
Simple Properties:
public int ParentLocationID {get; set;}
public int ChildLocationID {get; set;}
Navigation Properties:
public Location Location1 {get; set;}
public Location Location2 {get; set;}
Here, both the simple properties refer to the foreign key relationship with a Location Table. This information is available when I open the Designer.cs for the model.
But how can I know in the code that ParentLocationID is using which navigation property Location1 or Location2?
Upvotes: 0
Views: 84
Reputation: 1015
Use the ForeignKey annotation?
using System.ComponentModel.DataAnnotations.Schema;
public class MyModel
{
public int ParentLocationID {get; set;}
public int ChildLocationID {get; set;}
[ForeignKey("ParentLocationID")]
public Location Location1 {get; set;}
[ForeignKey("ChildLocationID")]
public Location Location2 {get; set;}
}
Is this what you mean?
Upvotes: 1
Reputation: 27039
But how can I know in the code that ParentLocationID is using which navigation property Location1 or Location2?
You can rename the navigation properties in the EDXM Model Browser. Just right click the navigation property and rename it so instead of Location1
, you can change it to ParentLocation
.
This will survive if you update the model from the database any number of times. However, if you totally delete the table from the EDMX and then re-create it, you will need to rename it again (but this is rarely done).
Upvotes: 0