我试图通过AutoMapper 8.0.0将某些实体映射到DTO,并将某些DTO映射到实体。
这是我将实体映射到DTO的AutoMapper定义。通过以下定义,我能够将实体映射到DTO。
但是,当我尝试通过反转Recipe和RecipeDTO将DTO映射到实体时,我无法进行映射。我出错了。
我应该如何定义自动映射器初始化才能实现这一点?
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Recipe, RecipeDTO>()
.ForMember(dest => dest.Directions, opt => opt.MapFrom(src =>
src.Directions))
.ForMember(dest => dest.Ingredients, opt => opt.MapFrom(src =>
src.Ingredients))
.ForMember(dest => dest.Categories, opt => opt.MapFrom(src =>
src.Categories.Select(i => i.Name)));
});
这些是我的实体和DTO's
public class Recipe
{
public override int Id { get; set; }
public string Title { get; set; }
public int DirectionId { get; set; }
public virtual Direction Directions { get; set; }
public override DateTime CreatedDate { get; set; }
public override string CreatedBy { get; set; }
public override DateTime? ModifiedDate { get; set; }
public override string ModifiedBy { get; set; }
public override bool IsDeleted { get; set; }
public virtual ICollection<Category> Categories{get;set;}
public virtual ICollection<Ingredient> Ingredients{get; set;}
}
public class Direction
{
public override int Id { get; set; }
public string Step { get; set; }
public int RecipeId { get; set; }
public virtual Recipe Recipe { get; set; }
public override DateTime CreatedDate { get; set; }
public override string CreatedBy { get; set; }
public override DateTime? ModifiedDate { get; set; }
public override string ModifiedBy { get; set; }
public override bool IsDeleted { get; set; }
}
public class Category
{
public override int Id { get; set; }
public string Name { get; set; }
public override DateTime CreatedDate { get; set; }
public override string CreatedBy { get; set; }
public override DateTime? ModifiedDate { get; set; }
public override string ModifiedBy { get; set; }
public override bool IsDeleted { get; set; }
public virtual ICollection<Recipe> Recipes { get; set;}
}
public class Ingredient
{
public override int Id { get; set; }
public string Name { get; set; }
public override DateTime CreatedDate { get; set; }
public override string CreatedBy { get; set; }
public override DateTime? ModifiedDate { get; set; }
public override string ModifiedBy { get; set; }
public override bool IsDeleted { get; set; }
public int AmountId { get; set; }
public virtual Amount Amount { get; set; }
public int RecipeId { get; set; }
public virtual Recipe Recipe { get; set; }
}
public class Amount
{
public override int Id { get; set; }
public string Quantity { get; set; }
public string Unit { get; set; }
public override DateTime CreatedDate { get; set; }
public override string CreatedBy { get; set; }
public override DateTime? ModifiedDate { get; set; }
public override string ModifiedBy { get; set; }
public override bool IsDeleted { get; set; }
public int IngredientId { get; set; }
public virtual Ingredient Ingredient { get; set; }
}
public class RecipeDTO
{
public string Title { get; set; }
public List<string> Categories { get; set; }
public List<IngredientDTO> Ingredients { get; set; }
public DirectionDTO Directions { get; set; }
}
public class DirectionDTO
{
public string Step { get; set; }
}
public class IngredientDTO
{
public string Name { get; set; }
public AmountDTO Amount { get; set; }
}
public class AmountDTO
{
public string Quantity { get; set; }
public string Unit { get; set; }
}