EF Core 2.0.0 One to One-or-Zero with Fluent Api

这一生的挚爱 提交于 2019-12-19 23:21:39

问题


In Fluent Api at EF Core 2.0.0, there aren't any methods HasRequired and HasOptional, and i have tow Models, Person and Employee:

    public class Person
    {
        public int Id { get; set; }

        public int EmployeeId { get; set; }
        public virtual Employee Employee { get; set; } // Optional
    }

    public class Employee
    {
        public int Id { get; set; }

        public int PersonId { get; set; }
        public virtual Person Person {get; set; } // Required
    }
  • Person May to have Employee: Optional
  • Employee Should have Person: Required

How to apply these convetions in database.


回答1:


You could just specify int? as EmployeeId property type.

BTW, no need to make navigation properties virtual.



来源:https://stackoverflow.com/questions/48394969/ef-core-2-0-0-one-to-one-or-zero-with-fluent-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!