We implement the Entity Framework inside the winforms application using DbContext / Code First and have the following question regarding the correct check / handling method when the object was deleted / updated in a different context.
For example, we have some auxiliary table data (for example, StateCodes), and the user can go to another and add / remove states as necessary. This form of the auxiliary editor uses its own DbContext and saves the changes after the user exits the form. Upon returning to the main form, the main context does not know the changes made to the database, so we want to reload the DbSet for the object. Unfortunately, it seems that if we delete the “MI” status code, it still exists in the local DbSet property with EntityState unchanged even after we call “Load” to enable everything.
Outside of the complete removal of the main context, it would be best to check and verify whether any objects are deleted from the database?
foreach (State state in db.States.Local) { DbEntityEntry entry = db.Entry(state); DbPropertyValues databaseValues = entry.GetDatabaseValues(); if (databaseValues == null) { db.States.Remove(state); } else { entry.OriginalValues.SetValues(databaseValues); } }
thanks for the help
source share