I have following entities
public class SchoolContext : DbContext
{
public DbSet<Address> Addresses { get; set; }
public DbSet<Employee> Employees { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Address Address { get; set; }
}
If I set relationship between Employee and Address with following Fluent API
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Option #1
modelBuilder.Entity<Employee>()
.HasRequired(s => s.Address)
.WithRequiredPrincipal(a => a.Employee);
// Option #2
modelBuilder.Entity<Employee>()
.HasOptional(s => s.Address)
.WithRequired(a => a.Employee);
}
Above two options create table structure exactly same, if so, what is different between two options. If I go with option #1, I thought Employee entity always should have address entity, but it was not. I was able to save Employee entity without address value.
Thanks in advance.