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