The purpose of this blog is to introduce you how to map relationship in Entity Framework Core.
- One to Many Relationship
- One to One Relationship
- Many to Many Relationship
One to Many Relationship:
In the same way as Entity Framework 6.x, EF core implements one-to-many mappings according to the conventional method.
We’ll explore how EF Core automatically creates a one-to-many relationship between Users and Ratings.
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
}
An entity’s reference navigation property Rating is present in these entities, and the entity’s collection navigation property is represented by ICollection<User>. As a result, a one-to-many relationship will be created.
One to One Relationship
Relationships between two entities could not be mapped one-to-one in EF 6. It is now possible with EF core, as it introduces a new method for mapping one-to-one relationships.
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
}
This represents the one-to-one relationship between the User entity and the Address entity. The User entity has its reference navigation property Address and the Address entity has its foreign key property UserId.
Many to Many Relationship.
Many-to-many relationships cannot be automatically configured with Entity Framework Core. With Fluent API and a Join Entity, you can easily accomplish this.
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; }
}
Users and skills are mapped many-to-many using the UserSkill Entity. In addition to the two foreign key references UserId and SkillId, the UserSkill entity has two navigation properties which refer to two foreign keys.
Users is a navigation property included in the Skill entity, while Skills is a navigation property included in the User entity. Next, we will use the Fluent API to join entity UserSkill with a foreign key configured as the composite primary key.
Pass ModelBuilder as a parameter to AppDBContext.cs’ OnModelCreating method.
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 creates a Composite Key based on SkillId and UserId.
Apply Migration
The database needs to be updated based on the changes to the models after the migration.
Add-Migration Relationship
Migration files are created based on model changes with the above command.
Update-Database
EF Core updates the database based on the migration using the above command.
Closing Thoughts:
As a result of reading this blog, you learned how to map relationships in Entityframework based on the following table:
- One to Many Relationship
- One to One Relationship
- Many to Many Relationship