0

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

vincentsty
  • 2,963
  • 7
  • 34
  • 51

1 Answers1

4

When you are configuring one-to-one relationships, Entity Framework requires that the primary key of the dependent (StudentContact) also be the foreign key. The proper way to achieve what you want could be this, but is using Data Annotations:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }

    public StudentContact StudentContact { get; set; }
}

public class StudentContact
{
    [Key, ForeignKey("Student")]
    public int StudentId { get; set; }
    public int Contact { get; set; }
    public Student Student { get; set; }
}
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • the example you mentioned above is for one to one relationship, but it does not work to create table for one to zero or one relationship (with column i shown) in the post. – vincentsty Mar 03 '15 at 13:48
  • 1
    @vincentsty No, it is one to zero or one relationship. octavioccl is right. – Jenish Rabadiya Mar 03 '15 at 13:49
  • @JenishRabadiya is right, The `StudentContact` is optional to set in the `Student` entity – ocuenca Mar 03 '15 at 13:55
  • @JenishRabadiya how could the above code first generate a one to zero or one relationship that adhere to the table column as i mentioned in the post. What i intend to achieve here is an autoincrement primary key on the StudentContact with an additional foreign key column that relates to student. My initial look on the above code first will result in non increment value of StudentID(primary key of student contact), meaning that the primary key in StudentContact it may be 2, 4, 7 if Student with ID 1, 3, 5, 6 does not have contact – vincentsty Mar 03 '15 at 14:04
  • 1
    In one-to-one relation, one end must be principal and second end must be dependent. Principal end (in your case, `Student`) is the one which will be inserted first and which can exist without the dependent one. Dependent end (`StudentContact`) is the one which must be inserted after the principal because it has foreign key to the principal, that is also its PK. You are going to have the same PK values in both tables – ocuenca Mar 03 '15 at 14:08
  • @vincentsty why do you want that auto incremented ids only? any specific reason? This is recommended approach. any ways you can always go for applying unique constraint on `StudentId` of `StudentContact `. – Jenish Rabadiya Mar 03 '15 at 14:09
  • @octavioccl i see, so in strict sense, the above table structure that i mentioned is not known as one to zero or one relationship? – vincentsty Mar 03 '15 at 14:13
  • @JenishRabadiya no specific reason, just personal feel that primary key should be increment always, so the choice that octavioccl mentioned is the recommended approach? – vincentsty Mar 03 '15 at 14:14
  • @octavioccl Yes. specifically we don't have any reason to apply unique constraint to StudentId column :) – Jenish Rabadiya Mar 03 '15 at 14:17