4

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.

Sina Lotfi
  • 3,044
  • 1
  • 23
  • 30
  • I strongly recommend reading [Relationships](https://learn.microsoft.com/en-us/ef/core/modeling/relationships) section of the documentation. In your case, it would be enough to remove the `EmployeeId` from `Person`. – Ivan Stoev Jan 23 '18 at 08:32
  • I have read this section, i want to know about one-to-one relationship, Employee is optional for Person but every Employee should have Person – Sina Lotfi Jan 23 '18 at 08:57
  • How about **Required and Optional Relationships** and **Other Relationship Patterns - One-to-one** parts from the link? And *Blog -> BlogImage* sample - it's exactly like your case. – Ivan Stoev Jan 23 '18 at 09:02
  • @SinaLotfi did you find any answer to solve the question ?? – hassan moradnezhad Feb 02 '19 at 10:33

1 Answers1

1

You could just specify int? as EmployeeId property type.

BTW, no need to make navigation properties virtual.

Ivan Zverev
  • 327
  • 2
  • 14