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

entity-framework entity-framework-core

Question

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
4
1/23/2018 9:02:58 AM

Accepted Answer

You could just specify int? as EmployeeId property type.

BTW, no need to make navigation properties virtual.

1
1/23/2018 5:48:21 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow