2
votes

I have a simple object with a default constructor. The objects I'm mapping to and from are both defined exactly the same and I've configured a mapper for them. Everything works fine getting the object from the database.

public class Tag
{
    public Guid ProjectId { get; set; }
    public Guid TagId { get; set; }
    public string Name { get; set; }
}

If I call Mapper.Instance.Map(tagFrom, tagTo); everything works fine, but if I call dbContext.Tags.Persist().InsertOrUpdate(tag); I get this error.

Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters

AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.

AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. Tag -> Expression1 (Destination member list) AKS.Common.Models.Tag -> System.Linq.Expressions.Expression1[[System.Func`2[[AKS.AppCore.Entities.Tag, AKS.AppCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Boolean, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] (Destination member list)

Unmapped properties:

Parameters

No available constructor.

Seems like it's saying I have no default constructor, but clearly there is one. Anyone know what I'm doing wrong?

Package Versions: AutoMapper 8.0.0 AutoMapper.Collections 5.0.0 AutoMapper.Collections.EntityFrameworkCore 0.2.0

1
There is no Persist or InsertOrUpdate in AutoMapper. The problem is with whatever that other code does. - Lucian Bargaoanu
Are you using AutoMapper.Collection.EntityFrameworkCore? What version? Also the exception shows different entity from the post - TopicEdit, and also Expression<Func<TopicEdit, bool>>, so make sure the code you post reproduces the issue. - Ivan Stoev
I updated the question with the error message for mapping Tag objects. I get the same error with all objects, but have been working with the Tag since it's simple. I also added the versions of the Automapper packages I'm referencing - Jeremy Hutchinson
I was able to reproduce it. It goes away if you add CreateMap<Tag, Tag>() to AM configuration. However there seems to be some equivalency expression bug, so the feature is not working at all (does wrong things because the expression is GuidX == GuidX, e.g. true, so FirstOrDefault() will return random item rather than the one that needs to be updated/deleted). Consider posting an issue to their tracker. - Ivan Stoev
Could you share that repro? I'm trying to create a unit test that reproduces, and possibly see if I can submit a PR. But I'm having a hard time reproducing outside my larger project. - Jeremy Hutchinson

1 Answers

1
votes

This may be a bug in AutoMapper.Collections.EntityFrameworkCore, or it may have been caused by my own misuse.

I was configuring my Mapper with the following code

var cfg = new MapperConfigurationExpression();

cfg.AddCollectionMappers();
cfg.UseEntityFrameworkCoreModel<MyDbContext>();

cfg.CreateMap<TagDto, TagEntity>().ReverseMap();

Then I was trying to validate my mapping config with this code before intializing the mapper.

var config = new MapperConfiguration(cfg);
config.AssertConfigurationIsValid();

Mapper.Initialize(cfg);

If I remove the lines where I create a MapperConfiguration and use that to AssertConfigurationIsValid() then calls to InsertOrUpdate() work.

I also found that I can call AssertConfigurationIsValid() if I initialize my mapper first, then call that method on the Mapper.Instance

Mapper.Initialize(cfg);
Mapper.Configuration.AssertConfigurationIsValid();