System.DateTime on 'T MaxInteger[T](System.Collections.Generic.IEnumerable`1[T])' violates the constraint of type T for .NET 7 using AutoMapper 11.0.1
Asked Answered
C

1

25

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'.
Chinookan answered 8/12, 2022 at 12:39 Comment(2)
If you vote down please say why. Very hard to improve questions otherwiseChinookan
Thank you for the versioning information. I also had this issue and in disappeared when I updated to 12.0.0.Atavistic
C
21

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);
Chinookan answered 8/12, 2022 at 12:52 Comment(2)
Brilliant, in my case this also made the trick config.ShouldMapMethod = (m => false);Rata
Thanks! Also helped solve an issue when running on visual studio for mac. Posting here in case someone else runs into the same issue. The error was being thrown in custom middleware await _next(context); - which was confusing. Solved by adding the cfg.Internal().MethodMappingEnabled = false; to program.cs in .NET6 web apiMouton

© 2022 - 2024 — McMap. All rights reserved.