0

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

2
  • 1
    its clear from the error, that while you are saving the employee object, the BranchId is not populated Commented Nov 26, 2017 at 12:04
  • Monah , if it was clear, he probably would t have asked.. as Chris Tarrant says "it's only easy if you know the answer" Commented Nov 26, 2017 at 12:08

1 Answer 1

1

You're attempting to save employee details that reference a branch that doesn't exist. This is either because you haven't set the branch id at all, or you have set it to a number that is not present in the branch table Set the employees branch id to a branch that already exists. If no branches exist, create one. You should be able to create a branch and an employee simultaneously in code so long as the employee correctly refers to the branch. Entity framework should save the branch first so that it exists at the point it comes to save the employee

Sign up to request clarification or add additional context in comments.

3 Comments

Ignore my previous comment please. Thanks both, very helpful. I am going to sweat a bit over that as I have actually set the branch id and populated it but at least I know where to look at. Thanks again.
Given that it's the DB giving that error, not EF it may be helpful to turn on some logging of what SQL EF is running and when; then you can see what the statement is that gives rise to the error. It's about 10 seconds after that that you realise EF is trying to save "mr test employee of branch id -1" that you'll suddenly remember the test/debug code you wrote, a hundred lines up (or some similar gotcha) :)
Understood and very useful, I actually did not think about the logging option :). Many thanks again indeed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.