I\'m having issues getting an optional one-to-many relationship to work.
My model is:
public class Person
{
public int Identifier { get; set; }
You can try this:
this.HasOptional(s => s.Department)
.WithMany(s => s.Members)
.HasForeignKey(s => s.MemberOfDepartment);
modelBuilder.Entity<Department>().HasMany(x => x.MemberOfDepartment).WithOptional();
Also try this:
public class Person
{
public int Identifier { get; set; }
public int DepartmentIdentifier {get; set;}
public virtual Department Department { get; set; }
}
public class Department
{
public int Identifier { get; set; }
public virtual IList<Person> Members { get; set; }
}
EF Person config using Fluent API:
this.HasRequired(p => p.Department).WithMany(d => d.Members).HasForeignKey(p =>
p.DepartmentIdentifier);
this.Property(p => p.DepartmentIdentifier).IsRequired();