The child entity needs a unique key to associate itself with the parent. The key usually holds the value of the principal key to mark its relationship with the parent and is termed a foreign key. In our blogging system, the BlogId property of the Post entity holds the reference to the parent unique key/principal key to denote the relationship with the Blog entity:
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Summary { get; set; }
public DateTime PublishedDateTime { get; set; }
public string Url { get; set; }
public long VisitorCount { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ModifiedAt { get; set; }
public int CreatedBy { get; set; }
public int ModifiedBy { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
public int AuthorId { get; set; }
public User Author { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public ICollection<TagPost> TagPosts { get; set; }
public ICollection<Comment> Comments { get; set; }
}
In database terminology, we would still call the property a foreign key. The Post table design is shown as follows. In the design, the highlighted BlogId field would be the foreign key of the table:
We have seen the foreign key, which defines the relationships built using the dependent entity. Let's explore navigation properties, which define the direction or nature of relationships, in the next section.