AutoMapper/AutoMapper.Collection.EFCore

Unmapped members were found exception after using MapperConfiguration

hutchcodes opened this issue · 2 comments

If I run the following code where TagEntity and TagDto both just have Guid Id and string Name properties, the InsertOrUpdate() throws an exception claiming unmapped members have been found and suggests that it can't find a constructor, even through Mapper.Instance.Map<AppCore.Entities.Tag>(tag); succeeds with no problems. After digging for a while to try to see if I could get this into a small reproducable state I noticed that if I remove the line where I create a MapperConfiguration then everything succeeds.

var cfg = new MapperConfigurationExpression();

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

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

Mapper.Initialize(cfg);

var db = new MyDbContext();

var tagDto = new TagDto
{
    TagId = Guid.NewGuid(),
    Name = "Test"
};

db.Tags.Persist().InsertOrUpdate(tagDto);

After a bit more testing I found that if I replace

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

Mapper.Initialize(cfg);

with

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

Things work as expected. It's just something about validating the config before initializing the mapper that causes things to breakdown.

This sounds as a issue for https://github.com/AutoMapper/AutoMapper repo, the mapper and configuration is from the AM package.