Foreign key

We could use a different naming convention for a foreign key and still map that foreign key against the navigation property (using the ForeignKey data annotation does the trick). In a relationship, a navigation property is not required in the dependent entity, meaning that having a single navigation property on a principal entity is more than sufficient:

In the following code, the custom foreign key is mapped against the navigation property:

    public class Blog
{
... // code removed for brevity
public ICollection<Post> Posts { get; set; }
}
public class Post
{
... // code removed for brevity
public int BlogSomeId { get; set; }
[ForeignKey("BlogSomeId")]
public Blog Blog { get; set; }
}

The following image illustrates that the custom foreign key is mapped against the navigation property:

We have modified the foreign key to BlogSomeId and configured it against the navigation property which is the Blog of the Post entity, and it still works as expected.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset