Introduction
- 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 relationship between entities.
A one-to-one relationship happens when the primary key of one table becomes a foreign key in another table. Entity framework enforces the primary key of the first table should participate in the primary key of the second table. In this relationship, only one record or zero records are present on either side of the tables.
Understand One to One relationship
To understand the one-to-one relationships, I have created two entities: Employee and EmployeeDetails. One employee record has only one detailed record. Here EmployeeId is the primary key for the Employee table and the same key is present on the primary key and foreign key of the EmployeeDetail table.
Through an association, an entity can be related to the other entities in the entity framework. The relation between the entities contains two ends which 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 relation 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 another side. InverseProperty attribute can help us to resolve this issue.
Example
Employee POCO class

- [Table("Employee")]
- public partial class Employee
- {
- [DatabaseGenerated(DatabaseGeneratedOption.None)]
- public int EmployeeId
- {
- get;
- set;
- }
- [Required]
- [StringLength(50)]
- public string FirstName
- {
- get;
- set;
- }
- [Required]
- [StringLength(50)]
- public string LastName
- {
- get;
- set;
- }
- [StringLength(50)]
- public string MiddleName
- {
- get;
- set;
- }
- public virtual EmployeeDetail
- {
- get;
- set;
- }
- }
- [Table("EmployeeDetail")]
- public partial class EmployeeDetail
- {
- [Key, ForeignKey("Employee")]
- [DatabaseGenerated(DatabaseGeneratedOption.None)]
- public int EmployeeId
- {
- get;
- set;
- }
- [StringLength(10)]
- public string Gender
- {
- get;
- set;
- }
- [StringLength(255)]
- public string EmailAddress
- {
- get;
- set;
- }
- [StringLength(50)]
- public string PhoneNumber
- {
- get;
- set;
- }
- public virtual EmployeeEmployee
- {
- get;
- set;
- }
- }
- public partial class EFTestModel: DbContext
- {
- public EFTestModel(): base("name=entities")
- {}
- public virtualDbSet < Employee > Employees
- {
- get;
- set;
- }
- public virtual DbSet < EmployeeDetail > EmployeeDetails
- {
- get;
- set;
- }
- protected override void OnModelCreating(DbModelBuildermodelBuilder)
- {}
- }
- static void Main(string[] args)
- {
- //Configure One to One Relationship in Entity Framework Using Code First Approach
- Employee employee;
- using(var context = newEntityModel.EFTestModel())
- {
- employee = context.Employees.FirstOrDefault();
- Console.WriteLine("Employee Details");
- Console.WriteLine("Employee Name:" + string.Join(" ", newobject[]
- {
- employee.FirstName, employee.LastName
- }));
- Console.WriteLine("Employee Email Address:" + employee.EmployeeDetail.EmailAddress);
- }
- Console.ReadLine();
- }

- protected override void OnModelCreating(DbModelBuildermodelBuilder)
- {
- modelBuilder.Entity < Customer > ().HasKey(p => p.CustomerId);
- modelBuilder.Entity < Customer > ().HasOptional(e => e.CustomerDetail).WithRequired(e => e.Customer);
- }
- [Table("Customer")]
- public partial class Customer
- {
- [DatabaseGenerated(DatabaseGeneratedOption.None)]
- public int CustomerId
- {
- get;
- set;
- }
- [Required]
- [StringLength(50)]
- public string FirstName
- {
- get;
- set;
- }
- [Required]
- [StringLength(50)]
- public string LastName
- {
- get;
- set;
- }
- [StringLength(50)]
- public string MiddleName
- {
- get;
- set;
- }
- public virtual CustomerDetailCustomerDetail
- {
- get;
- set;
- }
- }
- [Table("CustomerDetail")]
- public partial class CustomerDetail
- {
- [Key]
- [DatabaseGenerated(DatabaseGeneratedOption.None)]
- public int CustomerId
- {
- get;
- set;
- }
- [StringLength(255)]
- public string EmailAddress
- {
- get;
- set;
- }
- [StringLength(50)]
- public string PhoneNumber
- {
- get;
- set;
- }
- public virtual CustomerCustomer
- {
- get;
- set;
- }
- }
- static void Main(string[] args)
- {
- //Configure One to One Relationship in Entity Framework Using Code First Approach
- Customer c;
- using(var context = newEntityModel.EFTestModel())
- {
- c = context.Customers.FirstOrDefault();
- Console.WriteLine("Customer Details");
- Console.WriteLine("Customer Name:" + string.Join(" ", newobject[]
- {
- c.FirstName, c.LastName
- }));
- Console.WriteLine("Customer Email Address:" + c.CustomerDetail.EmailAddress);
- }
- Console.ReadLine();
- }


Sonu ChaudharyPosted Feb 25, 2016, 6:35 AM
good one!
Ankit BansalPosted Jan 10, 2016, 2:47 AM
nice one..keep sharing..
Santhakumar MunuswamyPosted Jan 9, 2016, 1:58 AM
Thanks for nice article. Keep it up