简体   繁体   中英

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

 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. Following is the code that I am using to Map the Dish to 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

As you can see in the Exception is says Missing map configuration.

Add the config to the 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.

so you can use this:

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

          cfg.CreateMap<Ingredients, IngredientsViewModel>();

     });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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