Updated February 14, 2023
Introduction to Entity Framework One to Many
Entity Framework One to Many relationships occurs when one table’s primary key develops into another table’s foreign key. The One to Many relationships in each row of the data in one table is linked to more records in the second table. The One to Many relationships is not the property of records, but it is the relationship of itself.
Overview of Entity Framework One to Many
Entity Framework One to Many relationships occurs while the primary key of one table becomes the foreign key of another table. When the foreign key is defined in the table represents many ends of the relationship. The One to Many relationships in a relational database occurs when the row of one table-1 is linked with many rows in table-2, but one of the rows in table-2 is the relationship to only one row in table-1.
Let’s understand One to Many relationships as follows:
To better understand one-to-many relationships, create two entities, PeopleAddress, and People; both entities contain records like peopleID, name, and peopleAddressID, address, city, state, and country, respectively. One person’s data has many address records; in our example, PeopleID is the primary key for the Person table, and that primary key takes part in the Primary and Foreign Key of the PeopleAddress table.
An entity can be related to other entities in the Entity Framework. The relationship between the entities includes two ends that describe the entity type and the diversity of the type. Those two ends of the relation are the Principle Role and the Dependent Role.
Configure Entity Framework One to Many
In Entity Framework Versions, we mostly do not need to configure the One to Many relationships because the relationship conventions complete all the conventions.
Let’s see the following examples:
Code First Conventions:
1. To add reference Navigation Property – Include the reference navigation property of the Author in the Book Entity Class.
Code:
public class AuthorMaster
{
public int Author_Id { get; set; }
public string Author_Name { get; set; }
}
public class BookMaster
{
public int Book_Id { get; set; }
public string Book_Title { get; set; }
public AuthorMaster Author { get; set; }
}
To include an Author Navigation property, it built the One to Many relationships between the two entities called AuthorMaster and BookMaster Table in the database by having the Foreign Key Author_Author_ID to Books Table.
2. To add the Collection Navigation Property – To accomplish the One-to-Many relationship by including the collection Navigation Property of the Book entity in the Author Entity Class.
Code:
public class AuthorMaster
{
public int Author_Id { get; set; }
public string Author_Name { get; set; }
public virtual ICollection<BookMaster> Books { get; set; }
}
public class BookMaster
{
public int Book_Id { get; set; }
public string Book_Title { get; set; }
}
3. To add Navigation Properties in both entities – To include the navigation properties at two entities also helps in the One to Many relationships.
For example, the author class contains the collection of Books, whereas the BookMaster class includes the navigation property of type Author.
Code:
public class AuthorMaster
{
public int Author_Id { get; set; }
public string Author_Name { get; set; }
public virtual ICollection<BookMaster> Books { get; set; }
}
public class BookMaster
{
public int Book_Id { get; set; }
public string Book_Title { get; set; }
public AuthorMaster Author { get; set; }
}
4. Fully-Defined Relationship – This relationship at two ends will build the One-to-Many relationship; let’s see with one example, the entity BookMaster contains the foreign key property Author_ID with its reference property AuthorMaster and the AuthorMaster has the collection of Books.
Code:
public class AuthorMaster
{
public int Author_Id { get; set; }
public string Author_Name { get; set; }
public virtual ICollection<BookMaster> BooksColl { get; set; }
}
public class BookMaster
{
public int Book_Id { get; set; }
public string Book_Title { get; set; }
public int Author_Id { get; set; }
public AuthorMaster Author { get; set; }
}
The entire convention creates the same result in the database.
Entity Framework One to Many Convention
In Entity Framework, several conventions are followed in the domain classes; it automatically produces the result in One to Many relationships between two tables in the database.
Let’s see one example of the conventions that built the one-to-many relationship.
Convention – 1
To begin the One to Many relationships between the student and Grade tables or entities, there are several students which those students are associated with one Grade. Therefore, it defines that every student entity points to the Grade. It is achieved by adding the reference navigation property of the Grade type in the Student Class as coded below.
Code:
public class StudentMaster
{
public int Student_Id { get; set; }
public string Student_Name { get; set; }
public GradeMaster Student_Grade { get; set; }
}
public class GradeMaster
{
public int Grade_Id { get; set; }
public string Grade_Name { get; set; }
public string Grade_Section { get; set; }
}
In this example, the StudentMaster class adds the reference navigation property of GradeMaster Class. Instead, there will be several students in a single grade, which results in a one-to-one relationship between the StudentMaster and grade master tables in the database.
Convention – 2
This convention adds the collection navigation property in the principal entity, as shown.
Code:
public class StudentMaster
{
public int Student_Id { get; set; }
public string Student_Name { get; set; }
}
public class GradeMaster
{
public int Grade_Id { get; set; }
public string Grade_Name { get; set; }
public string Grade_Section { get; set; }
public ICollection<StudentMaster> StudentsColl { get; set; }
}
In the example, the GradeMaster added the collection of Navigation Property of type ICollection<StudentMaster>. It returns the results as a One to Many relationships between the StudentMaster and the GradeMaster Entities. The simple way to configure One to Many relationships is by convention. The Entity Framework built the relationship if the entity contains the Navigation Property.
Entity Framework One to Many Fluent API
To configure the One to Many Relationship using the Fluent API, configuring the relationship using Fluent API in one place makes it controllable.
Look at the following example of two entity classes:
Code:
public class StudentMaster
{
public int Student_Id { get; set; }
public string Student_Name { get; set; }
public int S_CurrentGradeId { get; set; }
public GradeMaster S_CurrentGrade { get; set; }
}
public class GradeMaster
{
public int Grade_Id { get; set; }
public string Grade_Name { get; set; }
public string Grade_Section { get; set; }
public ICollection<StudentMaster> StudentsColl { get; set; }
}
To use Fluent API for configuring the relationship of two entities, we need to override the OnModelCreating method in the Context Class.
Code:
public class SchoolContext : DbContext
{
public DbSet<StudentMaster> StudentMaster { get; set; }
public DbSet<GradeMaster> GradeMaster { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// to configuring the relationship
modelBuilder.Entity<StudentMaster>()
.HasRequired<GradeMaster>(s => s.S_CurrentGrade)
.WithMany(gg => gg. StudentsColl)
.HasForeignKey<int>(s => s.S_CurrentGradeId);
}
}
Conclusion
This article has explained Entity Framework One to Many Relationships. Furthermore, it contains the configuration and conventions of EF One to Many relations.
Recommended Articles
This is a guide to Entity Framework One to Many. Here we discuss the introduction, configure entity framework one to many and fluent apis. You may also have a look at the following articles to learn more –