简体   繁体   English

如何避免System.Data.Entity.Infrastructure.DbUpdateException

[英]How to avoid System.Data.Entity.Infrastructure.DbUpdateException

I have this DbContext object which consists of - 我有这个DbContext对象,包括 -

- Employee
- CompanyAddress  (PK: AddressFirstLine, City)
Note: one Employee can have many CompanyAddress

Records are added to CompanyAddress table only if some address doesn't exists in CompanyAddress table. 仅当 CompanyAddress表中不存在某些地址时,记录才会添加到CompanyAddress表中。
If I have two DBContext objects from database say Snapshot1, Snapshot2. 如果我有两个来自数据库的DBContext对象,请说Snapshot1,Snapshot2。 Say when both these snapshots were taken, there were no records in CompanyAddress table. 假设在拍摄这两个快照时,CompanyAddress表中没有记录。 When changes were made to Snapshot1 and saved - records are written to CompanyAddress table. 当对Snapshot1进行更改并保存时 - 将记录写入CompanyAddress表。
When changes were made to Snapshot2 and saved using 当对Snapshot2进行更改并使用保存时

mydataContext.SaveChanges();

exception occurs: 异常发生:

System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries
System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_CompanyAddress'. Cannot insert duplicate key in object 'dbo.CompanyAddress'

It seems saving of Snapshot1 made Snapshot2 dirty because when they are saved back to database, both had same CompanyAddress records. 似乎保存Snapshot1会使Snapshot2变脏,因为当它们保存回数据库时,两者都有相同的 CompanyAddress记录。

What other call/settings I can make on dbContext object to avoid this error? 我可以在dbContext对象上进行哪些其他调用/设置以避免此错误?

Thank you! 谢谢!

Your error has got nothing to do with the DbContext objects. 您的错误与DbContext对象无关。 Your problem is that you are trying to insert a record with duplicating primary key. 您的问题是您正在尝试插入具有重复主键的记录。 That is what your exception message says. 这就是你的异常消息所说的。

Look at how you create your CompanyAddress objects and what are the keys when you save them - this will give you the clues. 看看如何创建CompanyAddress对象以及保存它们时的键 - 这将为您提供线索。

Edit: And it is a bad idea to have primary key to be a natural key, ie you should not assign city and address as primary keys. 编辑:将主键作为自然键是一个坏主意,即您不应将城市和地址指定为主键。 You should have either Guid or Integer to be primary key that is not dependent on the information stored in DB. 您应该将Guid或Integer作为主键,而不依赖于存储在DB中的信息。

And to enforce uniqueness, before you save to DB, you check if that record exists, and can add a unique index to database table based on the unique constraints. 为了强制实现唯一性,在保存到数据库之前,检查该记录是否存在,并可以根据唯一约束向数据库表添加唯一索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException System.Data.Entity.Infrastructure.DbUpdateException问题 - System.Data.Entity.Infrastructure.DbUpdateException Issue 如何解决 System.Data.Entity.Infrastructure.DbUpdateException - How to solve System.Data.Entity.Infrastructure.DbUpdateException 发生System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException occurred MVC EF-System.Data.Entity.Infrastructure.DbUpdateException - MVC EF - System.Data.Entity.Infrastructure.DbUpdateException 递归树方法上的System.Data.Entity.Infrastructure.DbUpdateException - System.Data.Entity.Infrastructure.DbUpdateException on recursive Tree method 将数据添加到数据库(错误:System.Data.Entity.Infrastructure.DbUpdateException) - Adding Data to Database (Error: System.Data.Entity.Infrastructure.DbUpdateException) EntityFramework.dll 中发生“System.Data.Entity.Infrastructure.DbUpdateException” - 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll 发生类型为“ System.Data.Entity.Infrastructure.DbUpdateException”的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred 类型为“ System.Data.Entity.Infrastructure.DbUpdateException”的异常 - An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM