简体   繁体   English

具有子列表属性映射问题的自动映射器

[英]Automapper with Child List Property Mapping Issue

I am having following Models 我有以下模特

Models 楷模

public class Dish
{
    [Required]
    public Int64 ID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Description { get; set; }
    [Required]
    public double Price { get; set; }
    [Required]
    public DateTime From { get; set; }
    [Required]
    public DateTime To { get; set; }
    [Required]
    public bool IsAvailable { get; set; }
    [Required]
    public string MealImage { get; set; }
    [Required]
    public List<Ingredients> Ingredients { get; set; }
}


public class Ingredients
{
    [Required]
    public Int64 ID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public int Quantity { get; set; }
    [Required]
    public Int64 Dish_ID { get; set; }
    [ForeignKey("Dish_ID")]
    public virtual Dish Dish { get; set; }
}

Following is the ViewModel for them 以下是他们的ViewModel

 public class DishViewModel
{
    public Int64 ID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Description { get; set; }
    [Required]
    public double Price { get; set; }
    [Required]
    public DateTime From { get; set; }
    [Required]
    public DateTime To { get; set; }
    public bool IsAvailable { get; set; }
    public string MealImage { get; set; }
    [Required]
    public string IngredientsData { get; set; }

    public List<IngredientsViewModel> Ingredients { get; set; }
}

 public class IngredientsViewModel
{
    [Required]
    public string Name { get; set; }
    [Required]
    public int Quantity { get; set; }
    [Required]
    public Int64 Dish_ID { get; set; }
}

I am using Automapper for mapping between them. 我正在使用Automapper在它们之间进行映射。 Following is the code that I am using to Map the Dish to DishViewModel 以下是我用于将菜单映射到DishViewModel的代码

public DishViewModel Create(Dish dish)
    {
        Mapper.Initialize(cfg => cfg.CreateMap<Dish, DishViewModel>()
        .ForMember(s => s.Ingredients, c => c.MapFrom(m => m.Ingredients))
        );
        DishViewModel dishViewModel = Mapper.Map<DishViewModel>(dish);
        return dishViewModel;
    }

I am getting following error in above process 我在上面的过程中遇到了以下错误 映射错误

Can anybody please guide me what wrong I am doing in above process. 任何人都可以指导我在上面的过程中做了什么错。

Thanks 谢谢

You have to create a Mapping configuration for Ingredients , similar to the Mapping for Dish and DishViewModel 您必须为Ingredients创建Mapping配置,类似于Dish for DishDishViewModel

As you can see in the Exception is says Missing map configuration. 正如您在异常中所看到的那样,缺少地图配置。

Add the config to the Mapper.Initialize 将配置添加到Mapper.Initialize

Mapper.Initialize(

    // Here you are only adding one config.

cfg => cfg.CreateMap<Dish, DishViewModel>()
        .ForMember(s => s.Ingredients, c => c.MapFrom(m => m.Ingredients))

        );

Change to this: 改为:

Mapper.Initialize(cfg =>
    {
          cfg.CreateMap<Dish, DishViewModel>()
                .ForMember(s => s.Ingredients, c => c.MapFrom(m => m.Ingredients));

           cfg.CreateMap<Ingredients, IngredientsViewModel>();

     });

Also you don't need the following: 您也不需要以下内容:

.ForMember(s => s.Ingredients, c => c.MapFrom(m => m.Ingredients));

as the name of the properties are same, AutoMapper will automatically Map the Properties. 由于属性的名称相同,AutoMapper将自动映射属性。

so you can use this: 所以你可以使用这个:

Mapper.Initialize(cfg =>
    {
          cfg.CreateMap<Dish, DishViewModel>();

          cfg.CreateMap<Ingredients, IngredientsViewModel>();

     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM