AutoMapper实体到Dto的转换:映射类型错误



我的实体中有一些数据不应该对外公开。所以我想通过将实体映射到dto来导出它。我收到的数据类型为IList<Article>。我需要将其转换为IList<ArticleBasicDto>,但当我尝试这样做时,我遇到了一个错误。

我尝试以这种方式将IList<ArticleBasicDto>导出为目标,但失败了。既然我只是在学习,我不能达到结果,我该怎么办?

我这样做的原因是文章中有太多关于用户实体的信息。此外,JSON输出中的文章对象中有一个Category字段,属于该Category的文章将在该字段中再次列出。我正在尝试使用Dto来消除这些不必要的字段。

错误消息

处理请求时发生未处理的异常。AutoMapperMappingException:缺少类型映射配置或不支持的映射。

映射类型:文章->条款基础Blog.Entity.Concrete.Article->Blog.Entity.Dtos.ArticleBasicDtolambda_method178(闭包,Article,ArticleBasicDto,ResolutionContext)

AutoMapperMappingException:映射类型时出错。

映射类型:IList1 -> IList1System.Collections.Generic.IList1[[Blog.Entities.Concrete.Article, Blog.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IList1[[Blog.Entity.Dtos.ArticleBasicDto,Blog.Entities,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]lambda_method177(闭包,IList,ResolutionContext)

映射器配置文件

namespace Blog.Business.AutoMapper.Profiles
{
public class ArticleProfile : Profile
{
public ArticleProfile()
{
CreateMap<Article, ArticleBasicDto>().ForMember(dest => dest.Category, opt => opt.MapFrom(x => x.Category)).ForMember(dest => dest.User,
opt => opt.MapFrom(x => x.User));
}
}
}

Dtos

namespace Blog.Entities.Dtos
{
public class ArticleBasicDto
{
public string Title { get; set; }
public string Content { get; set; }
public string Thumbnail { get; set; }
public DateTime Date { get; set; }
public int ViewsCount { get; set; } = 0;
public int CommentCount { get; set; } = 0;
public UserBasicDto User { get; set; }
public CategoryBasicDto Category { get; set; }
}
}
namespace Blog.Entities.Dtos
{
public class CategoryBasicDto
{
public string Name { get; set; }
public string Description { get; set; }
public string Slug { get; set; }
}
}
namespace Blog.Entities.Dtos
{
public class UserBasicDto
{
public string Email { get; set; }
public string Username { get; set; }
public string Picture { get; set; }
}
}

经理GetAllBasic方法

public async Task<IDataResult<ArticleBasicListDto>> GetAllBasic()
{
var results = await _unitOfWork.Articles.GetAllAsync();
var mapping = _mapper.Map<IList<ArticleBasicDto>>(results);
return new DataResult<ArticleBasicListDto>(ResultStatus.Success, new ArticleBasicListDto()
{
Articles = mapping,
});
}

MVC层-Startup.cs

namespace Blog.MVC
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().AddRazorRuntimeCompilation().AddNewtonsoftJson(opt => opt.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore);
services.AddAutoMapper(typeof(Startup));
services.LoadMyServices();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "Admin",
areaName: "Admin",
pattern: "Admin/{controller=Home}/{action=Index}/{id?}"
);
endpoints.MapDefaultControllerRoute();
endpoints.MapControllers();
});
}
}
}

要解决此问题,您必须将startup类替换为ArticleProfile,如下面的services.AddAutoMapper(typeof(ArticleProfile ));

它将解决您的问题。

您需要为用户和类别定义映射

CreateMap<User,CategoryBasicDto>();
CreateMap<Category,CategoryBasicDto>();

最新更新