Invalid column name when using savechanges () in entity structure

So, here's the deal, I changed my database schema and changed the PK for one of my tables, and I deleted everything related to the old PK (FK link in other tables).

However, I have this exception when I insert a new object using the savechanges () method

ex = {"An error occurred while updating the records. For more information, see the internal exception." }

And internal exception

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

Audit_ID is an old PK.

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

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

this Invalid column name after matching

and nothing solved my problem, since I deleted all edmx and created a new one, it also did not work.

ps: I use the first approach to the database

+6
source share
3 answers

The solution did not work because the problem was in the SQL server database.

I ran SQL Profiler and followed the program insert statement, I found that the error was in one of the triggers of the table that has the name of the previous column, changed it to a new PK, and it finally worked.

So, if someone has a similar problem, the above links in the message may help or you should check if the problem is really on the database server.

+1
source

Delete / Delete the table from the ef model (make sure it is deleted by viewing the model browser view).

Recover the solution.

Make sure the table is saved. Also check the connection strings that you are referencing the correct db.

Add the table again using the update model tool.

EDIT: better read the question

0
source

Create your own ModelKeyBuilder class.

class MyOwnModelKeyBuilder { public static void BuildCompositeKeys(DbModelBuilder modelBuilder) { modelBuilder.Entity<YourEntity>() .Ignore(x=>x.Audit_ID) .HasKey(x=>x.NewPrimaryKey); } } 

And in your Model.Context.cs

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { MyOwnModelKeyBuilder.BuildCompositeKeys(modelBuilder); } 

It may not be just a solution, because I do not know the structure of the tables, but it can help you.

0
source

All Articles