There are a couple of likely causes, search Stack Overflow for “EntityStates.Modified”, this might be required in your case.

For me this was different, the cause was a table that didn’t have a primary key. Our code used to work then stopped working when we moved to Azure SQL Server.

We were making a change and then calling:

_context.SaveChanges();

And nothing happened! When I tried adding the EntityStates.Modified solution you can find generally recommended for this:

_context.Entry(item).State = EntityState.Modified;

I got a new message about not being able to track an item without a primary key!

So I added a primary key in SQL Server Management Studio:

Right click and add primary then save

Then updated my model and changed:

entity.HasNoKey();

To:

entity.HasKey(e => e.Id);

And this started working!

Last modified: November 17, 2022

Author

Comments

Write a Reply or Comment

Your email address will not be published.