Home > Blockchain >  EF conflict with PK trying to save to the DB
EF conflict with PK trying to save to the DB

Time:11-09

I have app with 3 entity models

public class Project
{
    public Guid ProjectId { get; set; }
    public string ProjectName { get; set; }
    public string ProjectInfo { get; set; }
    public Guid DepartMentId { get; set; }

    public ICollection<Employee> Employees { get; set; } = new List<Employee>();

    public Department Department { get; set; }
}

public class Employee
{
    public Guid EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PayrollNumber { get; set; }
    public int Seniority { get; set; }
    [Column(TypeName = "decimal(10,2)")]
    public decimal Salary { get; set; }
    public int Rank { get; set; }
    public int Hours { get; set; }
    public ICollection<Project> Project { get; set; }
}

public class Department
{
    public Guid DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    public string DepartmentInfo { get; set; }
    public int Rank { get; set; }
    public List<Project> Projects { get; set; } = new List<Project>();
}

I am create a new employee after created department and project like this

public void AddEmployee(string fname, string lname, string payNum, int seniority, decimal salary, int hours, string projname)
    {
        var proj = _projectController.GetProjects().Where(x => x.ProjectName == projname).SingleOrDefault();
        var projects = new List<Project>();
        projects.Add(proj);

        var emp = new Employee { EmployeeId = Guid.NewGuid(), FirstName = fname, LastName = lname, PayrollNumber = payNum, Seniority = seniority, Salary = salary, Hours = hours, Project = projects };

        _employeeController.AddEmployee(emp);
    }

AddEmployee method leads to SaveChanges(); method after all and I am getting this error SqlException: Violation of PRIMARY KEY constraint 'PK_Projects'. Cannot insert duplicate key in object 'dbo.Projects'. The duplicate key value is (10bc054b-b9a7-47da-eb14-08dac1b7a7fe). The statement has been terminated. How can I fix this?

CodePudding user response:

Make sure that a link table has been created for the link between an employee and his project. In the model you are providing, it appears that you have a "many to many" relationship between employee and project, meaning an employee can have many prjoect and a project can have many employee.

So a table name like "Project-Employee" must be there and store the ids of both end as foreign key and have it's own primary key. If it's not there, then of course, duplicate primary key are going to be inserted and this error occurs.

Can you check in the database if it is present ? If yes I suggest you code using it.

CodePudding user response:

you need to fix it by checking which columns are the primary keys in the database not C# project if e.g. ProjectId is the unique primary key then error happens when inserted twice, quess the c# code doesn't work except you use meta annotation to indicate which columns are the primary key(s) (using @Key) in a code-first approach using migrations.

  • Related