• About Me
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Advertise / Sponsor

DOTNET DETAIL

Learn Microsoft .NET Technologies

  • Home
  • Tutorials
    • Angular
      • Angular 5
      • Angular 6
    • ASP.NET Core
    • Azure
    • React
    • Vue
  • Books
  • Courses
  • Cloud Hosting
  • Interview Questions
You are here: Home / ASP.NET Core / Define one to many relationship in Entity Framework Code First

Define one to many relationship in Entity Framework Code First

September 28, 2019 by mebakar1005 Leave a Comment

In this tutorial, we are going to learn how to define one to many relationship in entity framework code first approach between two entities (domain classes) using entity framework 6. First-of-all, we will create two entities (domain classes) Employee.cs and Company.cs and then we will see different types to define a relation between two entities using by following conventions and then we will define by using fluent API configurations.

Let’s see how to define one-to-many relationship between the following Company and Employee entities where there can be many employees in one company.

Let’s understand the above code. In Line # 5, we have included a collection navigation property. It means one company have collection of employees. The above code will produce the result in database as you do see in the below screenshot.

Convention Two:

In this step, we will define one-to-many relationship by including reference navigation property of Company into Employee class as you do see in the below code.

Let’s understand the above code. In Line # 10, we have included reference navigation property. This above code will also produce the same result as you have seen in the convention. See the output of convention two in the below screenshot.

Convention Three:

In this step, we will include the navigation property in both classes as you do see in the below code. This will also define one-to-many relationship between two classes.

Let’s understand the above code. In Line # 5, we have included collection navigation property and then in Line # 11 we have included reference navigation property. This above code will also produce the result as you do see in the below screenshot.

Convention Four:

In this step, we will see the proper and fully defined way to define one to many relationship between two classes in entity framework code first approach. Let’s see in the below code.

Let’s understand the above code, In Line # 5, we have included collection navigation property. In Line # 11, we have added CompanyId as a foreign Key property and in Line # 12 we have included reference navigation property in Employee class. This above code will also create the one to many relationship as you do see below in the screenshot.

2- How to configure one-to-many relationship by using Fluent API 

In the above step, we have seen how to define one to many relationship using conventions in entity framework code first approach. In this step, we will see how to configure Fluent API to define relations between two entities. 

Note: - If you are following conventions, then you don’t need to configure Fluent API to define relations between two entities.

Let’s see we have two classes as you do see in the below code.

Now, go to context class and then add the modelBuilder within the OnModelCreating method as you do see in the below code.

Let’s understand the above code, In Line # 3, here we start with the Employee entity. In Line # 4 and 5 means we have one company with many employees.

Note: - it can also be configured by starting the other end of relationship as do see below code.

After configuring the above  relationship, then you will see the output as you do see in the below screenshot.

Configure the required relationship

In this step, we will see how to configure the required relationship using Fluent API. So, to do this, you just need to add IsRequired() on the relationship as you do see in the below code.

Configure Cascade delete

In this step, we will see how to implement cascade delete using Fluent API. Cascade delete means, if we delete the parent row then all the child rows should also be deleted automatically.

Let’s see how to do in the below code.

How to create a .NET Core 3.0 trimmed self-contained single executable app using VS Code
Entity Framework Core & Stored Procedure using Fluent API

Related

See also  How to implement Server-Side filtration in Angular & ASP.NET Core App

Filed Under: ASP.NET Core, Entity Framework Tagged With: ENTITY FRAMEWORK, Entity Framework Core, One-to-Many, Relationship

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Buy me a coffeeBuy me a coffee

Jobs

Dotnet Jobs

Join Us

Join Us

Subscribe to Blog via Email

Enter your email address to subscribe.

Recent Posts

  • Top LINQ interview questions and answers.
  • Difference between .NET Core and .NET Framework
  • Top 10 Programming Languages of the Future.
  • Top MVC Interview Questions & Answers
  • 20 best Data Science Books: Beginner to Advanced Level
  • 10 Best Machine Learning Laptops
Copyright © 2022