One to many relationship using entity framework

Configure One-to-Many Relationships using Fluent API in Entity Framework Core

one to many relationship using entity framework

Most of the samples in this article use a one-to-many relationship to Foreign key: The property(s) in the dependent entity that is used to store. If you want to create an one-to-many relationship between those two entities your Per Entity Framework Core offers a number of approaches to the creation and modification of one-to-many relationships.

The property s in the dependent entity that is used to store the values of the principal key property that the entity is related to. The property s that uniquely identifies the principal entity. This may be the primary key or an alternate key.

A navigation property that contains references to many related entities. A navigation property that holds a reference to a single related entity. When discussing a particular navigation property, this term refers to the navigation property on the other end of the relationship.

one to many relationship using entity framework

The following code listing shows a one-to-many relationship between Blog and Post Post is the dependent entity Blog is the principal entity Post. BlogId is the foreign key Blog. BlogId is the principal key in this case it is a primary key rather than an alternate key Post.

Blog is a reference navigation property Blog. Posts is a collection navigation property Post. Blog is the inverse navigation property of Blog.

One to Many Relationship in Entity Framework - tekTutorialsHub

A property is considered a navigation property if the type it points to can not be mapped as a scalar type by the current database provider. Note Relationships that are discovered by convention will always target the primary key of the principal entity. To target an alternate key, additional configuration must be performed using the Fluent API. Fully Defined Relationships The most common pattern for relationships is to have navigation properties defined on both ends of the relationship and a foreign key property defined in the dependent entity class.

If a pair of navigation properties is found between two types, then they will be configured as inverse navigation properties of the same relationship. No Foreign Key Property While it is recommended to have a foreign key property defined in the dependent entity class, it is not required.

You can also have a single navigation property and a foreign key property. Cascade means dependent entities are also deleted. ClientSetNull means that dependent entities that are not loaded into memory will remain unchanged and must be manually deleted, or updated to point to a valid principal entity.

For entities that are loaded into memory, EF Core will attempt to set the foreign key properties to null. See the Required and Optional Relationships section for the difference between required and optional relationships.


See Cascade Delete for more details about the different delete behaviors and the defaults used by convention. Data Annotations There are two data annotations that can be used to configure relationships, [ForeignKey] and [InverseProperty]. However, you may configure relationships using Fluent API at one place to make it more maintainable. Consider the following Student and Grade entity classes.

First, we need to start configuring with any one entity class. CurrentGrade specifies that the Student entity has required the CurrentGrade property. This will create a NotNull foreign key column in the DB.

Now, it's time to configure the other end of the relationship - the Grade entity. Students specifies that the Grade entity class includes many Student entities. Here, many infers the ICollection type property.

one to many relationship using entity framework

Now, if the Student entity does not follow the Id property convention for foreign key, then we can specify the name of the foreign key using the HasForeignKey method. CurrentGradeId ; specifies the foreign key property in the Student entity. Alternatively, you can start configuring the relationship with the Grade entity instead of the Student entity. The following code produces the same result as above.

Configure One-to-Many Relationships using Fluent API in Entity Framework Core

CurrentGradeId ; The above example will create the following tables in the database. Configure the NotNull ForeignKey using Fluent API In convention 1, we have seen that it creates an optional one-to-many relationship which in turn creates a nullable foreign key column in the database.

To make it a NotNull column, use the HasRequired method as shown below. For example, if Grade is deleted then all the students in that Grade should also be deleted automatically.