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.
Leave a Reply