Ahasanul Banna
Ahasanul Banna

Reputation: 123

Several foreign keys in same table EF Core 7

I have two entities CodeMaster and Employee. In Employee, I have more foreign key optional properties to navigate to the CodeMaster entity. When I insert data into Employee, I get this error:

Inner Exception 1:
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employees_CodeMasters_BloodGroupId". The conflict occurred in database "AproPayrollDB", table "dbo.CodeMasters", column 'Id'

Here is my CodeMaster class:

public class CodeMaster : AuditableEntity
{
    public long Id { get; set; }
    public long CodeTypeId { get; set; }

    public string Value { get; set; }

    public bool IsActive { get; set; }

    public string Remarks { get; set; }

    public virtual CodeType CodeType { get; set; }   
    public ICollection<Employee> Employees { get; set; }
}

Employee

public class Employee : AuditableEntity
{
    public long Id { get; set; }

    public string EmployeeFullName { get; set; }
    public string WorkName { get; set; }
    public string FatherName { get; set; }
    public string MotherName { get; set; }

    public string EmployeeId { get; set; } = null!;
    public long? EmpTypeId { get; set; }

    public long? DepartmentId { get; set; }
    public long? DesignationId { get; set; }

    public DateTime? Dob { get; set; }
    public int? Age { get; set; }

    public long? BloodGroupId { get; set; }

    public string AprosoftEmailId { get; set; }
    public string PersonalEmailId { get; set; }
    public string AprosoftSkypeId { get; set; }

    public string MsteamsId { get; set; }
    public string ContactNumber { get; set; }
    public string EmergencyContactPerson { get; set; }
    public long? RelationshipId { get; set; }
    public string EmergencyNumber { get; set; }
    public long? LocationOfRemoteWorkStatus { get; set; }
    public long? UpazilaId { get; set; }
    public long? DistrictId { get; set; }
    public long? DivisionId { get; set; }

    public DateTime? EnrollmentDate { get; set; }
    public DateTime? RetirementDate { get; set; }

    public long? EmpStatusId { get; set; }
    public string Remarks { get; set; }

    [ForeignKey(nameof(DepartmentId))]
    public virtual CodeMaster Department { get; set; }

    [ForeignKey(nameof(BloodGroupId))]
    public virtual CodeMaster BloodGroup { get; set; }

    [ForeignKey(nameof(DistrictId))]
    public virtual CodeMaster District { get; set; }
}

Fluent API

builder.Entity<Employee>()
              .HasOne(e => e.Department)
              .WithMany()
              .HasForeignKey(e => e.DepartmentId);

builder.Entity<Employee>()
            .HasOne(e => e.District)
            .WithMany()
            .HasForeignKey(e => e.DistrictId);

builder.Entity<Employee>()
            .HasOne(e => e.BloodGroup)
            .WithMany()
            .HasForeignKey(e => e.BloodGroupId);

I want to create relations between CodeMaster and Employee entities using DepartmentId, DistrictId, BloodGroupId and so on.

Where have I made a mistake?

Upvotes: 1

Views: 245

Answers (1)

vernou
vernou

Reputation: 7590

From the documentation Relationship navigations :

When using C# nullable reference types, reference navigations must be nullable for optional relationships...

In the OP's code :

public class Employee : AuditableEntity
{
    public long? BloodGroupId { get; set; }

    [ForeignKey(nameof(BloodGroupId))]
    public virtual CodeMaster BloodGroup { get; set; }

    ...
}

The foreign key property long? BloodGroupId is nullable, but the corresponding navigation property CodeMaster BloodGroup isn't. I don't know how EF Core interprete this inconsistency, but fix it can resolve the problem :

public class Employee : AuditableEntity
{
    public long? BloodGroupId { get; set; }

    [ForeignKey(nameof(BloodGroupId))]
    public virtual CodeMaster? BloodGroup { get; set; }

    ...
}

Upvotes: 0

Related Questions