Entity framework 6 ObjectMaterialized event and SaveChanges override

c# entity-framework-6 savechanges

Question

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:

  1. Since the flags are not linked to database columns, how come updating them resulted in a database change for the entity?

  2. 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?

  3. 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?

1
1
12/16/2016 11:08:20 PM

Popular Answer

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);
0
12/22/2016 8:17:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow