我是。net的新手,所以我决定使用。net Core,而不是学习“老方法”。我在这里找到了一篇关于为。net Core设置AutoMapper的详细文章,但是对于新手来说还有更简单的操作指南吗?
当前回答
对于使用。net 7的AutoMapper 11.0.1,我开始得到这个异常:
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'.
看这个问题:
系统。DateTime on 'T MaxInteger[T](System.Collections.Generic.IEnumerable ' 1[T])'违反了。net 7使用AutoMapper 11.0.1的T类型约束
这意味着我不能再使用services.AddAutoMapper(typeof(MappingProfile).Assembly);无一例外。
对于AutoMapper.Extensions.Microsoft.DependencyInjection我这样解决它:
services.AddAutoMapper(cfg => cfg.Internal().MethodMappingEnabled = false, typeof(MappingProfile).Assembly);
对于Blazor WebAssembly客户端,解决方案是这样的:
var mapperConfig = new MapperConfiguration(mc =>
{
//Needed for https://github.com/AutoMapper/AutoMapper/issues/3988
mc.Internal().MethodMappingEnabled = false;
mc.AddProfile(new MappingProfile());
});
//mapperConfig.AssertConfigurationIsValid();
IMapper mapper = mapperConfig.CreateMapper();
builder.Services.AddSingleton(mapper);
其他回答
我用这种方式解决了它(类似于上面,但我觉得这是一个更干净的解决方案)。net Core 3.x
创建MappingProfile.cs类并使用Maps填充构造函数(我计划使用一个类来保存所有映射)
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Source, Dest>().ReverseMap();
}
}
在Startup.cs中,添加下面的内容以添加到DI(程序集参数用于保存映射配置的类,在我的例子中,它是MappingProfile类)。
//add automapper DI
services.AddAutoMapper(typeof(MappingProfile));
在Controller中,像使用其他DI对象一样使用它
[Route("api/[controller]")]
[ApiController]
public class AnyController : ControllerBase
{
private readonly IMapper _mapper;
public AnyController(IMapper mapper)
{
_mapper = mapper;
}
public IActionResult Get(int id)
{
var entity = repository.Get(id);
var dto = _mapper.Map<Dest>(entity);
return Ok(dto);
}
}
在我的Startup.cs (Core 2.2, Automapper 8.1.1)
services.AddAutoMapper(new Type[] { typeof(DAL.MapperProfile) });
在我的数据访问项目中
namespace DAL
{
public class MapperProfile : Profile
{
// place holder for AddAutoMapper (to bring in the DAL assembly)
}
}
在模型定义中
namespace DAL.Models
{
public class PositionProfile : Profile
{
public PositionProfile()
{
CreateMap<Position, PositionDto_v1>();
}
}
public class Position
{
...
}
在最新版本的asp.net core中,你应该使用以下初始化:
services.AddAutoMapper(typeof(YourMappingProfileClass));
需要安装一个安装自动程序的包。
添加AutoMapper.Extensions.Microsoft.DependencyInjection包
之后AddAutoMapper将在服务中可用。
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
}
创建从Employee类到EmployeeDTO的映射器。
using AutoMapper;
public class AutomapperProfile: Profile
{
public AutomapperProfile()
{
//Source to destination.
CreateMap<Employee,EmployeeDTO>();
}
}
EmployeeController从Employee映射到EmployeeDTo
using System.Collections.Generic;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
[ApiController()]
public class EmployeeController : ControllerBase
{
private readonly IMapper _mapper;
public EmployeeController(IMapper mapper)
{
_mapper = mapper;
}
[HttpGet]
public IEnumerable<EmployeeDTO> GetEmployees()
{
/*
Assume it to be a service call/database call
it returns a list of employee, and now we will map it to EmployeeDTO
*/
var employees = Employee.SetupEmployee();
var employeeDTO = _mapper.Map<IEnumerable<EmployeeDTO>>(employees);
return employeeDTO;
}
}
Employee.cs供参考
using System.Collections.Generic;
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int Salary { get; set; }
public static IEnumerable<Employee> SetupEmployee()
{
return new List<Employee>()
{
new Employee(){EmployeeId = 1, EmployeeName ="First", Salary=10000},
new Employee(){EmployeeId = 2, EmployeeName ="Second", Salary=20000},
new Employee(){EmployeeId = 3, EmployeeName ="Third", Salary=30000},
new Employee(){EmployeeId = 4, EmployeeName ="Fourth", Salary=40000},
new Employee(){EmployeeId = 5, EmployeeName ="Fifth", Salary=50000}
};
}
}
EmployeeDTO.cs供参考
public class EmployeeDTO
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
}
对于使用。net 7的AutoMapper 11.0.1,我开始得到这个异常:
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'.
看这个问题:
系统。DateTime on 'T MaxInteger[T](System.Collections.Generic.IEnumerable ' 1[T])'违反了。net 7使用AutoMapper 11.0.1的T类型约束
这意味着我不能再使用services.AddAutoMapper(typeof(MappingProfile).Assembly);无一例外。
对于AutoMapper.Extensions.Microsoft.DependencyInjection我这样解决它:
services.AddAutoMapper(cfg => cfg.Internal().MethodMappingEnabled = false, typeof(MappingProfile).Assembly);
对于Blazor WebAssembly客户端,解决方案是这样的:
var mapperConfig = new MapperConfiguration(mc =>
{
//Needed for https://github.com/AutoMapper/AutoMapper/issues/3988
mc.Internal().MethodMappingEnabled = false;
mc.AddProfile(new MappingProfile());
});
//mapperConfig.AssertConfigurationIsValid();
IMapper mapper = mapperConfig.CreateMapper();
builder.Services.AddSingleton(mapper);