One to zero-or-one with HasForeignKey

前端 未结 2 779
不知归路
不知归路 2020-12-13 06:48

I have two models:

public class Person
{
    public virtual int Id { get; set; }
    public virtual Employee Employee { get; set; } // optional
}

public cla         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-13 07:13

    There's actually a better way to do this:

    HasKey(e => e.PersonId);
    
    HasRequired(e => e.Person)
        .WithOptional(p => p.Employee);
    

    Note that you no longer need the Employee.Id property with this approach, as it was superfluous in terms of a 1 to 0 or 1 relationship in the first place.

提交回复
热议问题