
One-to-one relationships, in which a single entity is associated with another single entity.One-to-many relationships, in which a single entity is associated with any number of other entities.To jump into examples for different kinds of relationships, see: Find out moreĮF supports many different types of relationships, with many different ways these relationships can be represented and configured. Now all these properties will behave coherently together as a representation of a single relationship between Blog and Post. For example: protected override void OnModelCreating(ModelBuilder modelBuilder) This is done automatically by EF when building a simple relationship like this, but can also be specified explicitly when overriding the OnModelCreating method of your DbContext.

The primary key property of Blog, Blog.Id, and the foreign key property of Post, Post.BlogId, can then be associated with the references ("navigations") between the entity types ( Blog.Posts and Post.Blog).

However, even when the properties are hidden, it is important to recognize that they still exist in the EF model. Primary and foreign key properties don't need to be publicly visible properties of the entity type. Once this mapping is made, EF changes the foreign key values as needed when the references between objects change, and changes the references between objects as needed when the foreign key values change.

This match determines which blog every post is related to. This column is "constrained" such that any value in the BlogId column of Posts must match a value in the Id column of Blogs. The Blogs primary key column Id is referenced by the BlogId foreign key column of the Posts table. In addition, the Blogs table is given a "foreign key" column. The value of the primary key uniquely identifies each post or blog. In this relational model, the Posts and Blogs tables are each given a "primary key" column. For example, using SQL Server or Azure SQL, the following tables can be used to represent our Post and Blog classes: CREATE TABLE (ĬONSTRAINT PRIMARY KEY (),ĬONSTRAINT FOREIGN KEY () REFERENCES () ON DELETE CASCADE) ĬONSTRAINT PRIMARY KEY ())

Relational databases represent relationships using foreign keys. In EF Core, the Blog.Posts and Post.Blog properties are called "navigations". This connection from Blog to Post and, inversely, from Post back to Blog is known as a "relationship" in EF Core. Likewise, the opposite direction of the same relationship can be represented as a collection of Post objects on each Blog: public class Blog In an object-oriented language like C#, the blog and post are typically represented by two classes: Blog and Post. For example, when modeling posts in a blog, each post is related to the blog it is published on, and the blog is related to all the posts published on that blog. Relationships in object modelsĪ relationship defines how two entities relate to each other. This document provides a simple introduction to the representation of relationships in object models and relational databases, including how EF Core maps between the two.
