Take an example of the following Student and StudentAddress entities.
Configure one-to-zero-or-one relationship using DataAnnotations:
public class Student
{
public Student() { }
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual StudentAddress Address { get; set; }
}
public class StudentAddress
{
[ForeignKey("Student")]
public int StudentAddressId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
public virtual Student Student { get; set; }
}
When StudentAddress entity does not follow conventions:
If, for example, StudentAddress entity does not follow the convention for PK i.e. different name for Id property then you need to configure it for PK as well. Consider the following StudentAddress entity which has property name StudentId instead of StudentAddressId.
public class Student
{
public Student() { }
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual StudentAddress Address { get; set; }
}
public class StudentAddress
{
[Key, ForeignKey("Student")]
public int StudentId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
public virtual Student Student { get; set; }
}
In the above example, we need to configure StudentId property as Key as well as ForeignKey. This will make StudentId property in StudentAddress entity as PK and FK both.
Configure One-to-Zero-or-One relationship using Fluent API:
When Student and StudentAddress follow the conventions:
Student and StudentAddress entities follow the default code-first convention for PrimaryKey. So, we don t need to configure them to define their PrimaryKeys. We only need to configure StudentAddress entity where StudentAddressId should be ForeignKey.
The following example sets one-to-zero or one relationship between Student and StudentAddress using Fluent API.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Student & StudentAddress entity
modelBuilder.Entity<Student>()
.HasOptional(s => s.Address) // Mark Address property optional in Student entity
.WithRequired(ad => ad.Student); // mark Student property as required in StudentAddress entity. Cannot save StudentAddress without Student
}
In the above example, Student entity is configured using HasOptional() method which indicates that the StudentAddress navigation property in Student entity is an optional (not required when saving Student entity). Then, WithRequired() method configures StudentAddress entity and make Student navigation property of StudentAddress as required (required when saving StudentAddress entity. It will throw an exception when StudentAddress entity is saving without Student navigation property). This will make StudentAddressId as ForeignKey also.
Thus, you can configure One-to-Zero-or-one relationship between two entities where Student entity can be saved without attaching StudentAddress object to it but StudentAddress entity cannot be saved without attaching an object of Student entity. This makes one end required.
When StudentAddress entity do not follow conventions:
Now, let s take an example of StudentAddress entity where it does not follow primary key convention i.e. have different Id property name than Id. Consider the following Student and StudentAddress entities.
public class Student
{
public Student() { }
public int StudentId { get; set; }
public string StudentName { get; set; }
public virtual StudentAddress Address { get; set; }
}
public class StudentAddress
{
public int StudentId { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public int Zipcode { get; set; }
public string State { get; set; }
public string Country { get; set; }
public virtual Student Student { get; set; }
}
So now, we need to configure StudentId property of StudentAddress for PrimaryKey of StudentAddress as well as ForeignKey as shown below.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure StudentId as PK for StudentAddress
modelBuilder.Entity<StudentAddress>()
.HasKey(e => e.StudentId);
// Configure StudentId as FK for StudentAddress
modelBuilder.Entity<Student>()
.HasOptional(s => s.Address)
.WithRequired(ad => ad.Student);
}
Configure One-to-One relationship using Fluent API:
We can configure One-to-One relationship between entities using Fluent API where both ends are required, meaning Student entity object must include StudentAddress entity object and StudentAddress entity must include Student entity object in order to save it.
Note:One-to-one relationship is technically not possible in MS SQL Server. It will always be one-to-zero or one. EF forms One-to-One relationships on entities not in DB.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure StudentId as PK for StudentAddress
modelBuilder.Entity<StudentAddress>()
.HasKey(e => e.StudentId);
// Configure StudentId as FK for StudentAddress
modelBuilder.Entity<Student>()
.HasRequired(s => s.Address)
.WithRequiredPrincipal(ad => ad.Student);
}
In the above example, modelBuilder.Entity().HasRequired(s => s.Address) makes Address property of StudentAddress is required. .WithRequiredPrincipal(ad => ad.Student) makes Student property of StudentAddress entity as required. Thus it configures both ends required. So now, when you try to save Student entity without address or StudentAddress entity without Student, it will throw an exception.
Reference :http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx