How to write a one-to-one--or-zero
relationship using fluent api? Can someone help me out correct what i have written. I am not sure if its correct.
The scenario : One Student Can have one or zero Address.
Student Model
public int Id{ get; set; }
public string Name{ get; set; }
public Address Address{ get; set; }
Address Model
public int Id { get; set; }
public string Street{ get; set; }
public int StudentId { get; set; }
public Student Student { get; set; }
What I tried:
builder.HasOne(u => u.Address)
.WithOne(b => b.Student)
.HasForeignKey<Address>(b => b.StudentId);
StudentId
in your Address Model should be declared as an int. – vinicius.ras Dec 26 '18 at 17:45