【发布时间】:2020-07-02 12:56:43
【问题描述】:
域对象
namespace Blog.Domain
{
public class Category : BaseEntity
{
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; } = new HashSet<Post>();
}
}
Dto 对象
namespace Blog.Application.DataTransfer
{
public class CategoryDto
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
}
个人资料
namespace Blog.Api.Core.Profiles
{
public class CategoryProfile : Profile
{
public CategoryProfile()
{
CreateMap<Category, CategoryDto>();
CreateMap<CategoryDto, Category>();
}
}
}
在该类别中,我获取了有关该类别和属于该类别的帖子的信息,但映射不起作用。如何解决?我需要 .ForMember 来映射集合吗?
public class EfGetOneCategoryQuery : IGetOneCategoryQuery
{
private readonly BlogContext _context;
private readonly IMapper _mapper;
public EfGetOneCategoryQuery(BlogContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public CategoryDto Execute(int search)
{
var category = _context.Categories.Include(x => x.Posts).Where(x => x.Id == search).FirstOrDefault();
if (category == null)
{
throw new EntityNotFoundException(search, typeof(CategoryDto));
}
var response = _mapper.Map<CategoryDto>(category);
return response;
}
}
【问题讨论】:
-
你好。欢迎来到堆栈溢出。您需要提供有关实际错误的详细信息。我们甚至不确定这是编译错误还是运行时错误。
-
请分享您的错误代码
标签: c# entity-framework asp.net-core entity-framework-core