top of page

Configure One-To-Many Relationships In Entity Framework Using Code First Approach

Updated: Sep 9, 2020

Introduction The relationship between tables is defined by using foreign keys in relational databases. The foreign key is a column or combination of columns which enforces a  relation between the data of two tables. The following three types of relationships are supported:

  1. One to One

  2. One to Many or Many to one

  3. Many to Many

All three type of relationships are supported by entity framework in code first approach. In this article we will cover one-to-many relationships between entities. A one-to-many relationship happens when the primary key of one table becomes foreign keys in another table and also this primary key should participate in primary key of second table. In this relationship only zero, one and more than one records are present on either side of tables.

Understand One to Many relationship To understand one-to-many relationships, I have created two entities: People and PeopleAddress. One people record has one or more Address records. Here PeopleId is primary key for People table and it also participate in primary key and foreign key of PeopleAddress table.

Through association an entity can be related to the other entities in the entity framework. The relation between the entities contains two ends which describe type of the entity and multiplicity of the type. The two ends of relation can be referred as principal role and dependent role. There are two ways to configure one-to-one relations between two entities in Entity Framework Using DataAnnotation Entity Framework Code First provides a set of data annotation attributes that can be applied on domain classes or the properties of domain classes. ForeignKey Attribute specifies the foreign key for the Navigation property in Entity Framework. As discussed earlier, a relationship in the entity framework always has two ends, a navigation property on each side and an Entity Framework that maps them together automatically by convention. If there are multiple relationships between the two entities, Entity Framework cannot handle the relationships. This is because Entity Framework doesn't know which navigation property map with which properties on other side. InverseProperty attribute can help us to resolve this issue. Example: People POCO class:

[Table("People")]  
public partial class People  
{  
 public People()  
        {  
 this.PeopleAddress = newHashSet < PeopleAddress > ();  
        }  
        [DatabaseGenerated(DatabaseGeneratedOption.None)]  
        [Key]  
    public int PeopleId  
    {  
 get;  
 set;  
    }  
    [Required]  
    [StringLength(50)]  
    public string FirstName  
    {  
 get;  
 set;  
    }  
    [Required]  
    [StringLength(50)]  
    public string LastName  
    {  
 get;  
 set;  
    }  
    public virtual ICollection < PeopleAddress > PeopleAddress  
    {  
 get;  
 set;  
    }  
}  

PeopleAddressPOCO class

[Table("PeopleAddress")]  
public partial class PeopleAddress  
{  
    [Key]  
    [Column(Order = 1)]  
    [DatabaseGenerated(DatabaseGeneratedOption.None)]  
    public int PeopleAddressId  
    {  
 get;  
 set;  
    }  
    [Column(Order = 2)]  
    [Key, ForeignKey("People")]  
    public int PeopleId  
    {  
 get;  
 set;  
    }  
    [Required]  
    [StringLength(100)]  
    public string AddressLine1  
    {  
 get;  
 set;  
    }  
    [Required]  
    [StringLength(100)]  
    public string AddressLine2  
    {  
 get;  
 set;  
    }  
    [StringLength(50)]  
    public string City  
    {  
 get;  
 set;  
    }  
    [StringLength(50)]  
    public string State  
    {   
 get;  
 set;  
    }  
    [StringLength(50)]  
    public string Country  
    {  
 get;  
 set;  
    }  
    public virtual PeoplePeople  
    {  
 get;  
 set;  
    }  
}  

Entity Context class

public partial class EFTestModel: DbContext  
{  
    public EFTestModel(): base("name=entities")  
    {}  
    public virtual DbSet < People > People  
    {  
 get;  
 set;  
    }  
    public virtual DbSet < PeopleAddress > PeopleAddress  
    {  
 get;  
 set;  
    }  
    protected override void OnModelCreating(DbModelBuildermodelBuilder)  
    {}  
}  

In the above example, People and PeopleAddress class have a one-to-many relationship. Here, PeopleId will become the primary key of the People table and we have used key and ForeignKey attributes for PeopleId property in PeopleAddress class in order to make primary key and foreign key. In highlighted text in above code, we have to pass People entity in ForeignKey attribute of PeopleAddress class. Thus, the code first creates a one-to-many relation between Employee and EmployeeDetail class using DataAnnotations attributes.


To test the above model, I have created a console application that first gets people entity data and then prints its all addresses data on screen.

static void Main(string[] args)  
{  
 //Configure One to Many Relationship in Entity Framework Using Code First Approach 
    People people;  
 using(var context = newEntityModel.EFTestModel())  
    {  
        people = context.People.FirstOrDefault();  
 int index = 1;  
        Console.WriteLine("People Details");  
        Console.WriteLine("Name:" + string.Join(" ", newobject[]  
        {  
            people.FirstName, people.LastName  
        }));  
        Console.WriteLine("Addresses");  
        Console.WriteLine("---------");  
 for each(var address inpeople.PeopleAddress)  
        {  
            Console.WriteLine(index + string.Join(", ", newobject[]  
            {  
                address.AddressLine1, address.AddressLine2, address.City, address.State, address.Country  
            }));  
            index += 1;  
        }  
    }  
    Console.ReadLine();  
}  

Output


Using Fluent API


For conventions of primary key and foreign key in code first, we can use "Fluent API". Using "HasKey" method, we can define the primary key. Using “HasForeignKey” method we can define ForeignKey. "HasRequired" or “WithRequired” method is used to make property non-nullable. Using these two methods of fluent API, we can define foreign key in code first. “HasMany” and “WithMany” method is used to define one-to-many or many-to-many relation in entity framework.


We can configure one-to-many relationships between People and PeopleAddress using Fluent API by the following code in model class:

protected override void OnModelCreating(DbModelBuildermodelBuilder)  
{  
   modelBuilder.Entity<StudentAddress>()  
   .HasRequired<Student>(s =>s.Student)  
   .WithMany(s =>s.StudentAddress)  
   .HasForeignKey(s =>s.StudentId);  
 
 
   // Alternate possible way 
   //modelBuilder.Entity<Student>() 
   // .HasMany<StudentAddress>(s =>s.StudentAddress) 
   // .WithRequired(s =>s.Student) 
   // .HasForeignKey(s =>s.StudentId); 
 
}  

The following are the class definitions for Student and StudentAddress.

[Table("Student")]  
public partial class Student  
{  
 public Student()  
        {  
 this.StudentAddress = newHashSet < StudentAddress > ();  
        }  
        [DatabaseGenerated(DatabaseGeneratedOption.None)]  
    public int StudentId  
    {  
 get;  
 set;  
    }  
    [Required]  
    [StringLength(50)]  
    public string FirstName  
    {  
 get;  
 set;  
    }  
    [Required]  
    [StringLength(50)]  
    public string LastName  
    {  
 get;  
 set;  
    }  
    public virtual ICollection < StudentAddress > StudentAddress  
    {  
 get;  
 set;  
    }  
}  
[Table("StudentAddress")]  
public partial class StudentAddress  
{  
    [DatabaseGenerated(DatabaseGeneratedOption.None)]  
    public int StudentAddressId  
    {  
 get;  
 set;  
    }  
    public int StudentId  
    {  
 get;  
 set;  
    }  
    [Required]  
    [StringLength(100)]  
    public string AddressLine1  
    {  
 get;  
 set;  
    }  
    [Required]  
    [StringLength(100)]  
    public string AddressLine2  
    {  
 get;  
 set;  
    }  
    [StringLength(50)]  
    public string City  
    {  
 get;  
 set;  
    }  
    [StringLength(50)]  
    public string State  
    {  
 get;  
 set;  
    }  
    [StringLength(50)]  
    public string Country  
    {  
 get;  
 set;  
    }  
    public virtual StudentStudent  
    {  
 get;  
 set;  
    }  
}  

To test the above model, I have created a console application and get first student and printed its all address on screen.

static void Main(string[] args)  
{  
 //Configure One to Many Relationship in Entity Framework Using Code First Approach 
    Student student;  
 using(var context = new EntityModel.EFTestModel())  
    {  
        student = context.Student.FirstOrDefault();  
 int index1 = 1;  
        Console.WriteLine("Student Details");  
        Console.WriteLine("Name:" + string.Join(" ", newobject[]  
        {  
            student.FirstName, student.LastName  
        }));  
        Console.WriteLine("Addresses");  
        Console.WriteLine("---------");  
 for each(var address instudent.StudentAddress)  
        {  
            Console.WriteLine(index1 + " " + string.Join(", ", newobject[]  
            {  
                address.AddressLine1, address.AddressLine2, address.City, address.State, address.Country  
            }));  
            index1 += 1;  
        }  
    }  
    Console.ReadLine();  
}  


Output


Summary


This article helps us to learn how to configure one-to-many relationships between entities in a code first approach using data annotation or fluent API. In the next article we will learn how to configure many-to-many relations in entity framework in code first approach.


Source: Paper.li

0 comments
bottom of page