I am trying to enforce a one to (zero or one) table relationship using code first/fluent api and the intended table is as below.
Student may only have a contact (StudentContact) or not But Every contact (StudentContact) must have a Student
StudentID StudentName
1 StudentA
2 StudentB
StudentContactID StudentContact StudentID
1 123456789 1
2 123456789 2
I tried to use
EntityName<Student>().HasOptional(x => x.StudentContact).WithRequired(l => l.Student)
but unfortunately it does not enforce a one relationship for StudentID column, meaning that StudentID column may contain duplicate value.
reference: One to zero/one relation in entity framework code first