I have a strange problem with the combination of EF Core 2.0 and AutoMapper 8.1.0.
As it would be very nontrivial to extract some meaningful snippet of actual code, I will describe the structure, that is simple enough:
I have a database entity model (POCO only) mapped to DB via EF Core Code First - defined using the fluent API. Above that there is a domain model. Between these two layers there is AutoMapper configuration. When there is a need to map updated domain object to a simple entity one, it utilizes the overload
Mapper.Map(DomainObjectInstance, EntityObjectInstance, DomainType, EntityType);
to update the actual EntityObjectInstance tracked by EF Core.
It used to work pretty well. Unfortunately, there were some refactorings in the model and after that it stopped working together with the EF Core. After updating one of the domain object, it is being deleted from the database.
What I found out - after calling the Map
method, the ChangeTracker
in DBContext
detects a change in one of the important foreign keys - this is an entity that is also contained in a collection of another entity (typical 1-M relation). However, if I inspect the property, there is IsChanged = true
, but the actual CurrentValue
and OriginalValue
are the same.
Also interesting is that the CurrentValue == OriginalValue
returns false
but CurrentValue.Equals(OriginalValue)
returns true
because of un/boxing.
This change and its detection is definitely caused during the Map
method. If I comment it out, the pipeline works fine. Although there is no any actual change in the data and I set the mapping for this property to be ignored, the change in EF Core is still detected. If I do a "manual mapping" and assign values from domain object to entity object myself, the issue disappears. But there are quite some fields and I want to benefit from autoMapper usage.