I'm using the Model First methodology, and I have a class called MyEntity that is abstract and has a few flags with the property [NotMapped]. MyEntity is the ancestor of all my entities. I put those flags within the ObjectMaterialized event handler to make it clear that the object is materialised and is associated with my application.
I use base inside of my SaveChanges() override. After calling SaveChanges(), the flags are reset. Despite their being no actual data changes, it seems to trigger the entity update to the database.
My inquiries are:
Since the flags are not linked to database columns, how come updating them resulted in a database change for the entity?
I made an effort to move those flags into partial classes as opposed to the abstract class that each object inherits. It seems that such flag modifications continue to result in database entity updates. How come?
After resetting the flag settings to their prior values, I also attempted to leave them unaltered. SaveChanges(). Even though this won't lead to a database change, my programme doesn't function properly. To avoid changing the database, this method essentially manipulates the entity or property State. But what negative effects may it have?
By switching the entity state to EntityState, I was able to overcome this issue. Unchanged. For my app, it works. However, there is still a problem with regard to how to alter the state.
Change the condition that created the issue in the following ways:
dbContext.Entry(entity.Entity).State = EntityState.Unchanged;
I must use the following code to modify the state:
ObjectStateEntry state = ((IObjectContextAdapter)dbContext).ObjectContext.ObjectStateManager.GetObjectStateEntry(entity.Entity); state.ChangeState(EntityState.Unchanged);