[英]Entity Framework error message DbUpdateException
As part of an assignment, I must create a web app using Entity Framework. 作为任务的一部分,我必须使用实体框架创建一个Web应用程序。 I am following the tutorial at https://docs.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 .
我正在https://docs.microsoft.com/zh-CN/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework- ASP.NET MVC应用程序的数据模型 。
There are four tables: Branch
, Department
, Employee
and Title
. 有四个表:
Branch
, Department
, Employee
和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.
System.Data.Entity.Infrastructure.DbUpdateException:'更新条目时发生错误。 See the inner exception for details.'
有关详细信息,请参见内部异常。 SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Employee_dbo.Branch_BranchId".
SqlException:INSERT语句与FOREIGN KEY约束“ FK_dbo.Employee_dbo.Branch_BranchId”冲突。 The conflict occurred in database “HumanResourcesWebApp1”, table “dbo.Branch”,column 'ID'.
数据库“ HumanResourcesWebApp1”的表“ dbo.Branch”的列“ 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. 我了解到,当被
Employee
表视为外键时,问题出在Branch
表的主键之间,但我不知道在哪里进行更改。
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 提前致谢
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.
这是因为您根本没有设置分支ID,或者您已将其设置为分支表中不存在的数字将雇员分支ID设置为已存在的分支。 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
实体框架应首先保存分支,以便在保存员工时就存在该分支
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.