12

Complete source;

// See https://aka.ms/new-console-template for more information
using AutoMapper;

Console.WriteLine("Hello, World!");

var mapperConfig = new MapperConfiguration(mc =>
{
    mc.AddProfile(new MappingProfile());
});
//mapperConfig.AssertConfigurationIsValid();

IMapper mapper = mapperConfig.CreateMapper();

var entity = new Entity() { Created = DateTime.Now };

var entityDto = mapper.Map<Entity, EntityDto>(entity);

Console.WriteLine("Test");

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Entity, EntityDto>().ReverseMap();
    }
}

public class Entity
{
    public Guid Guid { get; set; }

    public DateTime Created { get; set; }

    public string CreatedById { get; set; }

    public ApplicationUser CreatedBy { get; set; }

}

public class EntityDto
{
    public Guid Guid { get; set; }

    public DateTime Created { get; set; }

    public string CreatedById { get; set; }

}

public class ApplicationUser
{

}

I can make the code work by either removing public ApplicationUser CreatedBy { get; set; } from Entity or remove public DateTime Created { get; set; } from EntityDto.

Version:

This only happens for .NET 7 using AutoMapper 11.0.1. It will work with .NET 7 using AutoMapper 12.0.0 or with .NET 6 using AutoMapper 11.0.1. Given that our project is dependent on NuGet https://www.nuget.org/packages/Microsoft.AspNetCore.ApiAuthorization.IdentityServer/7.0.0#dependencies-body-tab (Blazor default NuGet when a project is created from Visual Studio with individual user accounts) that in turn uses https://www.nuget.org/packages/Duende.IdentityServer.EntityFramework.Storage/6.0.4#dependencies-body-tab I can not upgrade to AutoMapper 12.0.0 since the dependency there is AutoMapper (>= 11.0.0 && < 12.0.0)

I have tried to upgrade Duende.Identity Nugets manually before since there are issues from time to time but usually something ends up breaking with Microsoft.AspNetCore.ApiAuthorization.IdentityServer so I would prefer not to do that. Example:

https://github.com/dotnet/aspnetcore/issues/41897

Exception

System.ArgumentException: 'GenericArguments[0], 'System.DateTime', on 'T MaxInteger[T](System.Collections.Generic.IEnumerable`1[T])' violates the constraint of type 'T'.'

Inner Exception
VerificationException: Method System.Linq.Enumerable.MaxInteger: type argument 'System.DateTime' violates the constraint of type parameter 'T'.
Ogglas
  • 62,132
  • 37
  • 328
  • 418

1 Answers1

11

Found an answer for it. Searched through issues before posting but I searched for the complete exception and found nothing.

https://github.com/AutoMapper/AutoMapper/issues/3988#issuecomment-1140716814

using AutoMapper;
using AutoMapper.Internal;

var mapperConfig = new MapperConfiguration(mc =>
{
    mc.Internal().MethodMappingEnabled = false;
    mc.AddProfile(new MappingProfile());
});

Dependency injection:

services.AddAutoMapper(cfg => cfg.Internal().MethodMappingEnabled = false, typeof(MappingProfile).Assembly);
Ogglas
  • 62,132
  • 37
  • 328
  • 418