Getting Start with Entity Framework Core 3.1 – Part III


Introduction:

   In this blog you will learn about the relationship mapping in Entity Framework Core.

  1. One to Many Relationship
  2. One to One Relationship
  3. Many to Many Relationship

This is a continuation of my last blog.

One to Many Relationship

EF core follows the same conventional method for one to many mappings as like Entity framework 6.x .

Let’s see how EF Core automatically configure a one to many relationships between User and Rating entities.

public class User
{     
  public int Id { get; set; }
  public string Name { get; set; }
  public Rating Rating { get; set; 
} // One to Many Relationship      
}
public class Rating
{              
  public int RatingId { get; set;}
  public string RatingLevel { get; set;}
  public ICollection<User> Users { get;set;} //One to Many Relationship
    }

The User entity has its reference navigation property Rating and on the other end the Rating entity also includes a collection navigation property ICollection<User>. This will create a one to many relationships.

One to One Relationship

EF core introduces the conventional method for one to one mappings which means it automatically map the one to one relationship between two entities which is not supported in EF 6.x or prior.

public class User
    {
 public User()
     {
      Address = new Address();
      }
 public int Id { get; set;}
 public string Name { get; set;}
 public Rating Rating { get; set;} 
 //One to Many Relationship
 public Address Address { get; set;} // One to One Relationship
     }
 public class Address
    {
  public int AddressId {get;set;}
  public string Street {get; set;}
  public string City {get; set;}
  public string State {get; set;}
  public string Country {get;set;}
  public int UserId { get; set; }
  public User User { get; set; } 
  //One to One Relationship
    }

The User entity has its reference navigation property Address and on the other end the Address entity also includes a navigation property User and the foreign key property UserId .This will create a one to one relationship.

Many to Many Relationship.

  There is no convention process in Entity Framework Core which automatically configure a many to many relationships. But it can be easily achieve through Fluent API with a Join Entity.

 public class User
    {
        public User()
        {
               UserSkills = new List<UserSkill>();
               Address = new Address();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public Rating Rating { get; set; } // one to Many Relationship
        public List<UserSkill> UserSkills { get; set; } // Many to Many Relationship 
        public Address Address { get; set; } // One to One Relationship
    }
public class Skill
    {
        public Skill()
        {
            UserSkills = new List<UserSkill>();
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public List<UserSkill> UserSkills { get; set; }// Many to Many 
  mapping 
    }
   public class UserSkill
    {
        public int UserId { get; set; }
        public int SkillId { get; set; }
        public User User { get; set; }
        public Skill Skill { get; set; }
    }

The UserSkill Entity is a Join entity to implement the many to many mapping between User and Skill entities. The UserSkill entity includes the two foreign key references UserId and SkillId, then two reference navigation properties User and Skill.

The Skill entity include the navigation property Users on the other end the User entity include the Skills navigation property. Next step is to configure the foreign key as composite primary key in the joining table which will be created based on joining entity UserSkill through Fluent API.

Override the OnModelCreating method in AppDBContext.cs file by passing ModelBuilder as a parameter.

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
   modelBuilder.Entity<UserSkill>().HasKey(s => new { s.SkillId, s.UserId 
         });
        }

modelBuilder.Entity<UserSkill>().HasKey(s => new { s.SkillId, s.UserId });  => This statement configure SkillId and UserId as a Composite Key.

Apply Migration

Now its time to apply the migration and update the database based on the changes in models.

Add-Migration Relationship

The above command is used to create a migration file based on model changes.

Figure 1: Add Migration

Update-Database

The above command is used to update the database by EF Core based on the migration.

Figure 2: Database Structure

Summary:

From this blog, you learned about relationship mapping in Entityframework as listed below

  • One to Many Relationship
  • One to One Relationship
  • Many to Many Relationship

Source Code – https://github.com/gowthamece/EFCore

Happy Coding!!!

gowthamk91

2 thoughts on “Getting Start with Entity Framework Core 3.1 – Part III

Leave a Reply to arunCancel reply

Discover more from Gowtham K

Subscribe now to keep reading and get access to the full archive.

Continue reading