Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection clear with equalitycomparision #79

Closed
ste4net opened this issue Mar 15, 2018 · 5 comments
Closed

Collection clear with equalitycomparision #79

ste4net opened this issue Mar 15, 2018 · 5 comments

Comments

@ste4net
Copy link

ste4net commented Mar 15, 2018

Source/destination types

    public class GroupPerUser
    {
        public long UserProfileId { get; set; }
        public UserProfile UserProfile { get; set; }
        public long GroupId { get; set; }
        public VehicleGroup Group { get; set; }
    }
    public class UserProfile : IdentityUser<long>, IDomainEntity
    {
        public virtual List<GroupPerUser> EnabledGroups { get; set; }
    }

    public class UserProfileVMTest : UserProfile
    {

    }

Mapping configuration

services.AddAutoMapper(c => { c.AddCollectionMappers(); c.AddProfile<MyMapperProfile>(); });
[...]
 CreateMap<GroupPerUser, GroupPerUser>()
                .EqualityComparison((g1, g2) => g1.UserProfileId == g2.UserProfileId && g1.GroupId == g2.GroupId);

Version: 6.2.2

Expected behavior

From my understanding using AddCollectionMappers collections have to be updated depending on equality comparision.

Actual behavior

In this case i see that collection is first cleared then filled. This produce an issue with EF changeTracker.

Steps to reproduce

      protected IMapper Mapper
        {
            get
            {
                if (_mapper == null)
                    _mapper = _serviceProvider.GetService<IMapper>();
                return _mapper;
            }
        }
[...]
var debugPlan = Mapper.ConfigurationProvider.BuildExecutionPlan(typeof(UserProfileVMTest), typeof(UserProfile));
Mapper.Map<UserProfileVMTest, UserProfile>(Model, Entity);
            
@TylerCarlson1
Copy link
Member

It should as long as your CreateMap is called in the MyMapperProfile. I honestly haven't tried the BuildExecutionPlan to see if it's in sync or not. Also I'm not 100% sure when you map a class to itself it will mess up the equality logic or not. AutoMapper doesn't allow that by default and it's more of a hack in this instance.

Another thing could be that services.AddAutoMapper could have an issue with registering it. I'm not super familiar with how that works.

@TylerCarlson1
Copy link
Member

Oh I might know what the issue might be. You have to set EnabledGroups to a value on constructor or parameter initialization.

public class UserProfile : IdentityUser<long>, IDomainEntity
    {
        public virtual List<GroupPerUser> EnabledGroups { get; set; } = new HashSet<GroupPerUser>();
    }

This should fix the updating problems in Entity Framework

@ste4net
Copy link
Author

ste4net commented Mar 17, 2018

Thank you very much.
I use often this approach when i have simple entity avoiding to create child DTO.
But... sorry it was not an Autmapper issue i have been messed up by the fact that it was happening only using mapping function but going a little bit deeper i saw that it was not Automapper deleting items it was EfCore calling dbCtx.Entry(tmpEnt)
It was a stupid things related to USerProfile property of GroupPerUser.
Source was null and obviously Automapper set null to destination than EFCore seeing that the property has been changed to null treat it like a removed entity and remove it from collection .
Simply solved asking Automapper to ignore that property
CreateMap<GroupPerUser, GroupPerUser>() .EqualityComparison((g1, g2) => g1.UserProfileId == g2.UserProfileId && g1.GroupId == g2.GroupId).ForMember(g => g.UserProfile, m => m.Ignore());

Thank you for your help.

@ste4net ste4net closed this as completed Mar 17, 2018
@TylerCarlson1
Copy link
Member

For your DTO's it's recommended not to have circular references, because it causes things like that. Also causes issues with projection and setting a max depth to stop the circular reference.

Best would be to make a new GroupPerUserVM and not include UserProfile property and that should cause the least amount of problems going forward.

@ste4net
Copy link
Author

ste4net commented Mar 19, 2018

Sure, thank you . I usually avoid circular references excluding property from Json serialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants