Configure One to Many Relationship in Entity Framework Core

A one-to-many relationship is the most commonly used relationship while developing database-oriented applications. EF Core identifies one-to-many relationships based on the conventions. You can create a one-to-many relationship by defining entities in different ways. In the following example, a Customer and a DeliveryAddress entities are used to explain it. They are connected by a one-to-many relationship as one customer can have more than one delivery address. One way to tell EF Core about this relationship is adding a collection of DelivaryAddress in the Customer entity as follows.

public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }      
        public ICollection<DeliveryAddress> DeliveryAddresses { get; set; }
    }

 

 public class DeliveryAddress
    {
        public int Id { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }

Here, no property is declared for the foreign key, hence Entity Framework Core will generate a shadow property named CustomerId and map it to a nullable CustomerId field in the DeliveryAddress table. 

You will get the same results if you add a reference navigation property of the Customer entity in the DelivaryAddress entity as follows.

public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }      
        public ICollection<DeliveryAddress> DeliveryAddresses { get; set; }
    }
 public class DeliveryAddress
    {
        public int Id { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public Customer Customer { get; set; }
    }

Here also, a foreign key property is not declared in the DeliveryAddress entity. So EF Core will generate a shadow property as explained in the previous example. For the above two approaches, the referential integrity constrain is  No Action. i.e., if you perform an update or delete in the Customer table, it will throw an error.

Fully defined relationship

A fully defined relationship is the one where the navigation properties are defined on both ends of the relationship and a foreign key property is defined in the dependent entity class.

 

public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }      
        public ICollection<DeliveryAddress> DeliveryAddresses { get; set; }
    }
public class DeliveryAddress
    {
        public int Id { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public int CustomerId { get; set; }
        public Customer Customer { get; set; }
    }

Here, the foreign key property is added to the DelivaryAddress entity. In this case, the foreign key will not be nullable, and the referential integrity constraint will be cascade on delete. i.e., when a record in the customer table is deleted, the related records from the DeliveryAddress table will also be deleted.  

To make the foreign key field in the database nullable, make the foreign key property in the DelivaryAddress entity nullable, as shown below.

 public class DeliveryAddress
    {
        public int Id { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public int? CustomerId { get; set; }
        public Customer Customer { get; set; }
    }

These are the conventions that automatically create a one-to-many relationship in the database. You can configure one-to-many relationships using Fluent API also.

 


Search