Page 1 of 1

Entry state modified even if no property was modified (DbUpdateConcurrencyException)

Posted: Mon 27 Apr 2020 12:58
by raubv0gel
If I set a property of an entity Album, let’s say Album.Name = "Greatest Hits", but Album.Name == "Greatest Hits" was true before, DbContext.Entry<Album>(Album).State is set to EntityState.Modified. This seems to be wrong, because there is no change.

If I call DbContext.SaveChanges() later, I get an odd DbUpdateConcurrencyException. If you catch this exception, you can compare database values and local values (see https://docs.microsoft.com/en-us/ef/ef6 ... oncurrency). But these values do not differ!

This seems to be a bug. I can try to create a minimal working example if needed.

Re: Entry state modified even if no property was modified (DbUpdateConcurrencyException)

Posted: Thu 30 Apr 2020 20:32
by Shalex
The issue is caused by change tracking implementation in EF engine. Refer to https://stackoverflow.com/questions/135 ... -is-set-b/.

Re: Entry state modified even if no property was modified (DbUpdateConcurrencyException)

Posted: Wed 06 May 2020 11:59
by raubv0gel
Thank you. That’s strange …

If I set FoundRows = true in a MySqlConnectionStringBuilder instance, I don’t get the exception. Is setting FoundRows the right way / best practice?

Re: Entry state modified even if no property was modified (DbUpdateConcurrencyException)

Posted: Fri 08 May 2020 11:35
by Shalex
"FoundRows=true;" in the connection string is the right way.
FoundRows specifies whether the provider will return the number of rows matched by the WHERE condition of the UPDATE statement instead of the rows actually changed as the number of changed rows.

Re: Entry state modified even if no property was modified (DbUpdateConcurrencyException)

Posted: Mon 18 May 2020 13:12
by raubv0gel
Thank you!