I feel silly asking this, but, I just can’t work out how to create a one to zero or one relationship, giving both an entityID and entity property.
Take the following example:
public class base
{
public int ID {get;set;}
}
public class linked
{
public int ID {get;set;}
public string test {get;set;}
}
If I put public virtual linked linked {get;set;} into the base class, everything works as expected, I get a nullable 1 to 0 or 1 relationship.
However, I understand the benefits of using just an int/EntityID to store the relationship in combination with a virtual for lazy loading (e.g. public int linkedID {get;set;} in combination with the virtual above), but, I can’t seem to work out how to create this, using the same relationship.
I have tried using the fluent API Entity<base>().HasOptional(x=>x.linked).WithRequired(), which is what I thought it should be, and this creates the 0 to 0 or 1 relationship, but, it is not nullable, which I do not understand given the HasOptional.
I thought I had turned a corner in understanding the Entity Framework, but this has really thrown me back! I can not understand where I am going wrong. Can anyone please help?
Try using
The int you where using isn’t nullable so it would default to 0.