Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Column name when using savechanges() in entity framework

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).

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

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

And the inner exception is

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

the Audit_ID is the old PK.

I have tried this "Invalid column name" when trying to insert data into database using SQL

this Invalid column name when trying to add an entity to a database using DbContext

this Invalid column name after mapping

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

ps: I am using database first approach

like image 489
SaadK Avatar asked Sep 20 '16 08:09

SaadK


People also ask

How do I use SaveChanges in Entity Framework?

In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction. The following example demonstrates this.

What does the Dbcontext SaveChanges () method return?

Returns. The number of state entries written to the underlying database. This can include state entries for entities and/or relationships.

What is SaveChanges?

SaveChanges()Saves all changes made in this context to the database. This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database.


1 Answers

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

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.

like image 159
SaadK Avatar answered Oct 24 '22 05:10

SaadK