I have a relationship on project and employee entities. I need have added a one-to-many relationship using Entity Framework Core (one employee have many projects, but one project has one employee). The get, post, delete requests are working fine, but the update request is failing.
This is my project entity:
using HRM_API.Entity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HRM_API.Entities
{
public class Project
{
[Key]
public Guid ProjectID { get; set; }
[MaxLength(1000)]
public string? ProjectTitle { get; set; }
[MaxLength(1000)]
public string? Description { get; set; }
[Column("fk_emp_id")]
public Guid? employeeID { get; set; }
//Navigation Properties
public Employee? employee { get; set; }
}
}
This is my employee entity:
using HRM_API.Entities;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HRM_API.Entity
{
public class Employee
{
[Key]
public Guid EmpId { get; set; }
[MaxLength(200)]
public string? FirstName { get; set; }
[MaxLength(200)]
public string? MiddleName { get; set; }
[MaxLength(600)]
public string? FullName { get; set; }
// Navigation Properties
public ICollection<Project> projects { get; set; } = new List<Project>();
}
}
This is the relationship in the DbContext
:
//employee and project relationship one to many
modelBuilder.Entity<Employee>()
.HasMany(e => e.projects)
.WithOne(q => q.employee)
.HasForeignKey(q => q.employeeID)
.OnDelete(DeleteBehavior.SetNull);
When I am updating a project details it throws this exception:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
Microsoft.Data.SqlClient.SqlException (0x80131904): The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_projects_employees_fk_emp_id". The conflict occurred in database "HRM-DB", table "dbo.employees", column 'EmpId'.
Additionally this is the updated request dto:
using System.ComponentModel.DataAnnotations;
namespace HRM_API.Models.Project
{
public class UpdateProjectDto
{
[Required]
[MinLength(4)]
[MaxLength(1000)]
public string? ProjectTitle { get; set; }
[Required]
public Guid EmpId { get; set; }
[Required]
[MinLength(4)]
[MaxLength(1000)]
public string? Description { get; set; }
}
}
This is the repository method for updating the project:
public async Task<Project> UpdateProjectByID(Guid projectID, Project updatedProject )
{
Project existing = await FindProjectByID(projectID);
existing.ProjectTitle = updatedProject.ProjectTitle;
existing.Description = updatedProject.Description;
existing.employeeID = updatedProject.employeeID;
await dbContext.SaveChangesAsync();
return existing;
}
Guide me how to overcome this error
The encountered error indicates that the issue stems from the fact that the employeeId assigned for the project does not exist in the database for any employee.
In this case, if the specified service method is triggered using an API and the value of "updatedProject.employeeID" provided in the request body does not exist for any employee, this error occurs.
Another possible scenario is, if this service method is triggered after creating an employee, and this newly created employee is being assigned to the project. If the employee has not yet been saved in the database, this could also lead to the same issue. Therefore, ensuring that such changes are saved after the employee has been successfully saved can prevent this situation.