AutoMapper不断抛出一个错误,指出我有未映射的属性(CustomerId,CreateDate,UpdateDate)。我正在尝试从我的数据库中检索结果,但由于某种原因,它认为这些字段没有被设置。我只是希望错误在此期间消失,所以我尝试在这3个字段上设置Ignore(),但是,我仍然收到错误。
我的startup.cs是我初始化的地方: Project.Api startup.cs
public void ConfigureServices(IServiceCollection services)
{
....
services.AddAutoMapper();
....
}
我在BaseService类中使用依赖注入集: Project.Service BaseService.cs
protected readonly Context Context;
protected readonly IMapper Mapper;
protected BaseService(Context context, IMapper mapper)
{
Context = context;
Mapper = mapper;
}
我在一个独立的类中设置AutoMapper: Project.Api DomainProfile.cs
public class DomainProfiles : Profile
{
public void CustomerProfile()
{
CreateMap<Customer, CustomerDto>()
.ForMember(dest => dest.CustomerId, opt => opt.Ignore())
.ForMember(dest => dest.CreateDate, opt => opt.Ignore())
.ForMember(dest => dest.UpdateDate, opt => opt.Ignore())
.ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.CustomerName))
.ForMember(dest => dest.CustomerShort, opt => opt.MapFrom(src => src.CustomerShort))
.ForMember(dest => dest.RecordStatus, opt => opt.MapFrom(src => src.RecordStatus));
}
}
并在我的服务中调用它: Project.Service CustomerService.cs
public CustomerService(Context context, IMapper mapper) : base(context, mapper) { }
protected override DbSet<Customer> EntitySet => Context.Customer;
protected override Expression<Func<Customer, bool>> SingleEntityPredicate(int id)
{
return entity => entity.CustomerId == id;
}
public async Task<ServiceDto<CollectionDto<CustomerDto>>> ReadAll(CollectionQueryParametersDto queryParameters)
{
var statusCode = HttpStatusCode.OK;
var errorList = new List<string>();
// Database work
var customers = await EntitySet
.AsNoTracking()
.ApplyQueryParameters(queryParameters)
.ToListAsync();
var dtos = customers.Select(c => Mapper.Map<CustomerDto>(c));
// Create JSON objects
return dtos
.WrapInCollectionResponse(statusCode, errorList);
}
}
答案 0 :(得分:1)
我的问题是我的DomainProfile没有设置为构造函数。为了解决这个问题,我不得不将CustomerProfile方法名称更改为DomainProfiles以匹配类名,并删除空白,它立即起作用。
发件人强>
public class DomainProfiles : Profile
{
public void CustomerProfile()
{
CreateMap<VipCustomer, CustomerDto>()
.ForMember(dest => dest.CustomerId, opt => opt.Ignore())
.ForMember(dest => dest.CreateDate, opt => opt.Ignore())
.ForMember(dest => dest.UpdateDate, opt => opt.Ignore())
.ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.CustomerName))
.ForMember(dest => dest.CustomerShort, opt => opt.MapFrom(src => src.CustomerShort))
.ForMember(dest => dest.RecordStatus, opt => opt.MapFrom(src => src.RecordStatus));
}
}
以强>
public class DomainProfiles : Profile
{
public DomainProfiles()
{
CreateMap<VipCustomer, CustomerDto>()
.ForMember(dest => dest.CustomerId, opt => opt.Ignore())
.ForMember(dest => dest.CreateDate, opt => opt.Ignore())
.ForMember(dest => dest.UpdateDate, opt => opt.Ignore())
.ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.CustomerName))
.ForMember(dest => dest.CustomerShort, opt => opt.MapFrom(src => src.CustomerShort))
.ForMember(dest => dest.RecordStatus, opt => opt.MapFrom(src => src.RecordStatus));
}
}