如何摆脱此运行时EF错误 [英] How can I get rid of this run time EF error

查看:266
本文介绍了如何摆脱此运行时EF错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其他信息:已成功提交对数据库的更改,但更新对象上下文时发生错误。 ObjectContext可能处于不一致状态。内部异常消息:保存或接受更改失败,因为多个类型为Project.Models.CertiStaging的实体;具有相同的主键值。确保显式设置的主键值是唯一的。确保在数据库和Entity Framework模型中正确配置了数据库生成的主键。使用实体设计器进行数据库优先/模型优先配置。使用'HasDatabaseGeneratedOption'流畅的API或'DatabaseGeneratedAttribute'进行代码优先配置。



我尝试过:



我有两个名为Certi和CertiStaging的数据库表,它们都有相同的主键。我不想删除那个主键,也不想改变我的模型中的任何东西。我怎么能摆脱它那个运行时EF错误?

解决方案

听起来你正试图保存与主键唯一性规则冲突的数据。看看你的数据是什么保存并查看是否存在您尝试保存的相同主键值的记录。如果存在,则数据无效,或者您需要创建更具说明性的主键以确保记录唯一性或更改要保存的数据。

Additional information: The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Saving or accepting changes failed because more than one entity of type 'Project.Models.CertiStaging; have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.

What I have tried:

I have two database tables named Certi and CertiStaging, both has same primary key. I don't want to remove that primary key nor want to alter anything in my models. How can I get rid of that run time EF Error?

解决方案

Sounds like you are trying to save data that conflicts the rules of the primary key uniqueness. Have a look at the data you are saving and see if there is a record of the same primary key values you are try to save. If so, the data is invalid so or you need to create a more prescriptive primary key to ensure record uniqueness or change the data you are saving.


这篇关于如何摆脱此运行时EF错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆