I have Person and Address. Address is optional. Please see below code
class Person
{
[Key]
public int PersonID { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
class Address
{
[Key, ForeignKey("Person")]
public int PersonID { get; set; }
public string City { get; set; }
}
Registration code is below:
modelBuilder.Entity<Address>(entity =>
{
entity.HasKey(z => z.PersonID);
entity.HasOne(p => p.Person)
.WithOne(a => a.Address)
.HasForeignKey<Person>(a => a.PersonId);
});
How should i change mapping to make Address optionable?