在实体框架中使用 savechanges() 时列名无效

人气:237 发布:2022-10-16 标签: asp.net-mvc entity-framework sql-server entity-framework-6 ef-database-first

问题描述

这就是交易,我已经更改了我的数据库架构,并更改了我的一个表的 PK,并且我已经删除了与旧 PK 相关的所有内容(另一个表中的 FK 引用).

So here is the deal, I have changed my database schema, and changed the PK for one of my tables and I have removed everything related to the old PK (FK reference in another tables).

但是,当我使用 savechanges() 方法插入新实体时出现此异常

However I have this exception when I insert a new entity using savechanges() method

ex = {"更新条目时发生错误.有关详细信息,请参阅内部异常."}

ex = {"An error occurred while updating the entries. See the inner exception for details."}

内部异常是

InnerException = {"无效的列名'Audit_ID'."}

InnerException = {"Invalid column name 'Audit_ID'."}

Audit_ID 是旧的 PK.

the Audit_ID is the old PK.

我试过这个无效列名称"尝试使用 SQL 将数据插入数据库时​​

这个 尝试使用 DbContext 将实体添加到数据库时列名无效

此映射后列名无效

并没有解决我的问题,所以当我删除了我的整个 edmx 并创建了一个新的时,它也没有用.

and nothing solved my issue, so as I deleted my whole edmx and created a new one also it didn't work.

ps:我使用的是数据库优先方法

ps: I am using database first approach

推荐答案

所有解决方案都不起作用,因为问题实际上出在 SQL Server 数据库中.

Non of the solutions worked because the problem was actually in the SQL server database.

我运行 SQL Profiler 并执行程序插入语句,我发现错误出在表的触发器之一中,该触发器具有 Previous 列名,将其更改为新的 PK,它终于起作用了.

I ran SQL Profiler and executed the program insert statement, I found out that the error is in one of the table's triggers which has the Previous column name, changed that to the new PK and it finally worked.

因此,如果有人遇到类似问题,帖子中的上述链接可能会有所帮助,或者您应该检查问题是否实际发生在数据库服务器上.

So if anyone have a similar problem, the above links in the post might help or you should check if the problem was actually happening the Database server.

746