Introduction
The relationship between tables is defined by using foreign keys in relational databases. The foreign key is a column or combination of columns that enforces a relationship between the data of two tables. The following three types of relationships are supported:- One to One
- One to Many or Many to one
- Many to Many
All three types of relationships are supported by the entity framework in the 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 the primary key of the second table. In this relationship only zero, one, and more than one recordsare present on either side of tables.
Understand One to Many relationships
To understand one-to-many relationships, I have created two entities: People and PeopleAddress. One person record has one or more Address records. Here PeopleId is the primary key for the People table and it also participates in the primary key and foreign key of the 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 that describe the type of the entity and multiplicity of the type. The two ends of relation can be referred to as the 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 to 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 the 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;
- }
- }
- [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;
- }
- }
- 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)
- {}
- }
- 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();
- }

- 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);
- }
- [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;
- }
- }
- 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();
- }


Sonu ChaudharyPosted Feb 25, 2016, 6:34 AM
good one!
Santhakumar MunuswamyPosted Jan 23, 2016, 2:17 AM
Thanks for nice article
Ankit BansalPosted Jan 22, 2016, 12:55 AM
keep sharing..thanks
Ankur MistryPosted Jan 21, 2016, 2:17 PM
Nice
Debasis SahaPosted Jan 21, 2016, 4:19 AM
Nice..
Arul RPosted Jan 21, 2016, 12:11 AM
Nice share
Jaipal ReddyPosted Jan 20, 2016, 11:38 PM
Thanks for share. .
Muhammad Aqib ShehzadPosted Jan 20, 2016, 2:17 PM
good one