web-dev-qa-db-fra.com

Collection d'objets cartographiques

J'essaie d'introduire Automapper dans une application pour la première fois, mais je reçois toujours une erreur indiquant que j'ai des arguments invalides.

Mon modele:

namespace StoreGradesLib.Models
{
    public class Store
    {
        [Key]
        public int StoreID { get; set; }

        [Required]
        [MaxLength(120)]
        public string StoreName { get; set; }

        [Required]
        [MaxLength(20)]
        public string StoreNumber { get; set; }

        [Required]
        [MaxLength(120)]
        public string ManagerName { get; set; }

        [Required]
        public long PhoneNumber { get; set; }

        [Required]
        public string AddressLine1 { get; set; }

        public string AddressLine2 { get; set; }

        [Required]
        public string Postcode { get; set; }

        [Required]
        public int WallArea { get; set; }

        [Required]
        public int FloorArea { get; set; }

        [Required]
        public int NumWindows { get; set; }

        [Required]
        public int NumDesks { get; set; }

        [Required]
        public int NumDoors { get; set; }

        [Required]
        public int StoreGradeID { get; set; }

        [Required]
        public bool Active { get; set; }

        public virtual StoreGrade StoreGrade { get; set; }

        [Timestamp]
        public Byte[] Timestamp { get; set; }
    }
}

Mon modèle de vue:

namespace StoreGradesLib.ViewModels
{
    public class StoreVM
    {
        public int StoreID { get; set; }
        public bool Active { get; set; }
        public Byte[] Timestamp { get; set; }

        [Required(ErrorMessage = "Store Name is required.")]
        [Display(Name = "Store Name")]
        public string StoreName { get; set; }

        [Required(ErrorMessage = "Store Number is required")]
        public string StoreNumber { get; set; }

        [Required(ErrorMessage = "Store Manager is required.")]
        [Display(Name = "Manager Name")]
        public string ManagerName { get; set; }

        [Required(ErrorMessage = "Contact Number is required.")]
        [Display(Name = "Phone Number")]
        public int PhoneNumber { get; set; }

        [Required(ErrorMessage = "Address Line 1 is required.")]
        [Display(Name = "Address Line 1")]
        public string AddressLine1 { get; set; }

        [Display(Name = "Address Line 2")]
        public string AddressLine2 { get; set; }

        [Required(ErrorMessage = "Postcode is required.")]
        public string Postcode { get; set; }

        [Required(ErrorMessage = "Must input wall area.")]
        [Display(Name = "Wall Area")]
        public int WallArea { get; set; }

        [Required(ErrorMessage = "Must input floor area.")]
        [Display(Name = "Floor Area")]
        public int FloorArea { get; set; }

        [Required(ErrorMessage = "Must input number of windows.")]
        [Display(Name = "Windows")]
        public int NumWindows { get; set; }

        [Required(ErrorMessage = "Must input number of desks.")]
        [Display(Name = "Desks")]
        public int NumDesks { get; set; }

        [Required(ErrorMessage = "Must input number of doors.")]
        [Display(Name = "Doors")]
        public int NumDoors { get; set; }

        [Required(ErrorMessage = "Store must have a grade.")]
        public StoreGrade StoreGradeID { get; set; }

        public string Address
        {
            get
            {
                return StoreName + " " + AddressLine1 + " " + AddressLine2 + " " +                 Postcode;
            }
        }
    }
}

Mappages créés:

CreateMap<Store, StoreVM>();
CreateMap<StoreVM, Store>();

Au sein de mon contrôleur, j'essaie de mapper une sélection de magasins vers storeVM. Je reçois actuellement mes magasins comme tel;

var stores = db.Store.Include(s => s.StoreGrade);
stores = from s in db.Store.Where(s => s.Active.Equals(true))
                 select s;

Je souhaite mapper la sélection de magasins vers StoreVM, j'ai essayé ce qui suit, mais je reçois un avertissement de paramètres non valides,

var VMstores = Mapper.Map<Store, StoreVM>(stores);

Je reçois l'erreur que la meilleure correspondance de méthode surchargée contient des arguments non valides.

Est-ce que quelqu'un peut-il me montrer la bonne direction?

38
Riddick

Vous mappez des collections, pas des entités uniques (IEnumerable<Store> à IEnumerable<StoreVM>), utilisez donc ce mappage

var VMstores = Mapper.Map<IEnumerable<Store>, IEnumerable<StoreVM>>(stores);
99
Sergey Berezovskiy

Mise à jour! Maintenant vous pouvez le faire:

var VMstores = stores.Project().To<StoreVM>();
10
Danilo Breda