Entity is not attached exception
Posted: Tue 17 Jul 2018 19:42
I´m using version 4.4.727 for Postgresql.
My app allows users to query the database, and navigate the resulting list.
When the user attempts to update or delete one of the records, the code tries to get a "fresh" version from the database; if some of the properties are changed (by another user) the operation is cancelled, the new value/s are displayed and the user is alerted.
To get the "current" version from the DB, I use
This code works fine a few times; but most often it throws "Entity is not attached".
Also, I modified the code, so if there are changes among instances, the new (read from db) instance is Attached.
Nevertheless, the error persists.
a) What am I doing the wrong way?
b) Is there another path to get the same results?
My first approach was
but this code don´t read the entity from the database, it gets the value from the Attached instances, so is not aware of changes.
TIA
My app allows users to query the database, and navigate the resulting list.
When the user attempts to update or delete one of the records, the code tries to get a "fresh" version from the database; if some of the properties are changed (by another user) the operation is cancelled, the new value/s are displayed and the user is alerted.
To get the "current" version from the DB, I use
Code: Select all
Entity currentInDb = context.Entities.GetOriginalEntityState(currentDisplayed);
This code works fine a few times; but most often it throws "Entity is not attached".
Also, I modified the code, so if there are changes among instances, the new (read from db) instance is Attached.
Nevertheless, the error persists.
a) What am I doing the wrong way?
b) Is there another path to get the same results?
My first approach was
Code: Select all
Entity currentindb = (from e in context.Entities
where e.Id == currentDisplayed.Id
select e).FirstOrDefault();
but this code don´t read the entity from the database, it gets the value from the Attached instances, so is not aware of changes.
TIA