0

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.

Jan Drozen
  • 894
  • 2
  • 12
  • 28

1 Answers1

0

I still don't know what exactly is wrong and why. But the reason is stuck somewhere between the EF Core and AutoMapper. The problem actually was that during the refactoring one of the properties was renamed. All of the settings, both EF entity model and AutoMapper mapping config were adjusted to match to the refactored state. I tried also to do all the mapping configurations with MemberList.None options and specify everything manually. Maybe the reason was the property got the same name like it's type (Project). But other props like this work fine.

However, there is no any issue with the mapping itself - it works or with EF Core itself. But together it simply does not work. (Un)fortunately, renaming the property helped this time.

In the comment Lucian suggested to use the EFCore extension for AutoMapper - and it did not help.

Jan Drozen
  • 894
  • 2
  • 12
  • 28