【问题标题】: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\'T MaxInteger[T](System.Collections.Generic.IEnumerable`1[T])\' 上的 System.DateTime 违反了使用 AutoMapper 11.0.1 的 .NET 7 类型 T 的约束
【发布时间】:2023-02-03 05:24:16
【问题描述】:

完整的来源;

// 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
{

}

我可以通过从 Entity 中删除 public ApplicationUser CreatedBy { get; set; } 或从 EntityDto 中删除 public DateTime Created { get; set; } 来使代码正常工作。

版本:

这仅发生在使用 AutoMapper 11.0.1 的 .NET 7 中。它将使用 AutoMapper 12.0.0 与 .NET 7 一起使用,或者使用 AutoMapper 11.0.1 与 .NET 6 一起使用。鉴于我们的项目依赖于 NuGet https://www.nuget.org/packages/Microsoft.AspNetCore.ApiAuthorization.IdentityServer/7.0.0#dependencies-body-tab(当使用个人用户帐户从 Visual Studio 创建项目时,Blazor 默认为 NuGet)反过来使用 https://www.nuget.org/packages/Duende.IdentityServer.EntityFramework.Storage/6.0.4#dependencies-body-tab 我无法升级到 AutoMapper 12.0.0,因为存在依赖关系 @ 987654323@ (>= 11.0.0 && < 12.0.0)

我之前曾尝试手动升级Duende.Identity Nugets,因为不时会出现问题,但通常某些事情最终会因Microsoft.AspNetCore.ApiAuthorization.IdentityServer而中断,所以我不想这样做。例子:

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

例外

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'.

【问题讨论】:

  • 如果您投反对票,请说明原因。否则很难改进问题

标签: c# .net nuget automapper automapper-11


【解决方案1】:

找到了答案。在发布之前搜索问题,但我搜索了完整的异常但一无所获。

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());
});

依赖注入:

services.AddAutoMapper(cfg => cfg.Internal().MethodMappingEnabled = false, typeof(MappingProfile).Assembly);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2016-11-16
    • 2022-12-15
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    相关资源
    最近更新 更多