asp.net - EntityFramework.dll 中发生了 'System.Data.Entity.Infrastructure.DbUpdateException' 类型的异常,但未在用户代码中处理

标签 asp.net asp.net-mvc entity-framework

我映射了两个表并在 MVC 中创建了脚手架。单击创建按钮时,在表单中填写数据后,出现以下错误。

An exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll but was not handled in user code

Additional information: A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.

最佳答案

当您有一个子类型表映射,但其主键设置为自动生成时,通常会发生此问题。这是有问题的,因为 EF 将根据根实体的键显式设置值。

如果以上确实是问题,那么要解决它,请将子表的“ Identity Specification ”设置为false。即删除 StoreGeneratedPattern="identity" 来自 EDMX。

关于asp.net - EntityFramework.dll 中发生了 'System.Data.Entity.Infrastructure.DbUpdateException' 类型的异常,但未在用户代码中处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24103916/

相关文章:

c# - 使 Firebird 与 Entity Framework Code First 和迁移一起工作

asp.net - 如何防止 ASP.Net Core Razor 中的 ViewData 内容编码?

c# - ASP.NET C# session 变量丢失

c# - 为什么我不应该在 ASP.NET MVC 3 中使用 TempData、Session 和 ViewBag?

c# - .NET MVC 3 如何循环模型状态错误并强制 ModelState.Valid 或禁用特定错误?

asp.net-mvc - 如何更改 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 中的 id 类型

entity-framework - EF 代码优先 : How to load related data (parent-child-grandchild)?

asp.net - Web 部署项目和 TeamCity

asp.net - 错误。 SQL 网络接口(interface),错误 : 26 - Error Locating Server/Instance Specified

c# - razor 页面上的 Dropzone 返回 400 状态代码