As part of an assignment, I must create a web app using Entity Framework. I am following the tutorial at https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application.
There are four tables: Branch, Department, Employee and Title.
After creating a method to seed the data I have run the application in debugging mode and got the following error message:
System.Data.Entity.Infrastructure.DbUpdateException: ‘An error occurred while updating the entries. See the inner exception for details.’ SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Employee_dbo.Branch_BranchId". The conflict occurred in database “HumanResourcesWebApp1”, table “dbo.Branch”,column ‘ID’. The statement has been terminated.
I have researched this message but I am not following the answers which I have found. I would be grateful for some direction. I am also adding the structure of the classes if it helps. I understand that the issue is between the primary key of the Branch table when seen as a foreign key by the Employee table but I do not know where to make changes.
Branch:
public int ID { get; set; }
public string Address { get; set; }
public int DepartmentID { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
Department:
public int ID { get; set; }
public string DepartmentName { get; set; }
public string DepartmentFunction { get; set; }
public virtual ICollection<Branch> Branches { get; set; }
Employee:
public int ID { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public double Salary { get; set; }
public DateTime StartingDate { get; set; }
public int BranchId { get; set; }
public int TitleId { get; set; }
Title:
public int ID { get; set; }
public string TitleName { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
Thanks in advance