// 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
{
}
我可以通过从
public ApplicationUser CreatedBy { get; set; }
中删除 Entity
或从 public DateTime Created { get; set; }
中删除 EntityDto
来使代码正常工作。
这只发生在使用 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#dependency-body-tab (从 Visual 创建项目时 Blazor 默认 NuGet具有个人用户帐户的 Studio),依次使用 https://www.nuget.org/packages/Duende.IdentityServer.EntityFramework.Storage/6.0.4#dependency-body-tab 我无法升级到 AutoMapper 12.0.0由于存在依赖关系 AutoMapper (>= 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'.
找到了答案。在发布之前搜索了问题,但我搜索了完整的异常,但什么也没找到。
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);
您是如何通过项目中的这些修复来更新 automapper 的版本的?