0

I have an problem in my ASP.net MVC Application.

How Can i map my Model to my ViewModel (with navigation propery as a collection)?

Sorry for uppcase, but it's Database first approach, and I can't change names in database

Model:

public partial class DB_USER
    {
        public int ID { get; set; }
        public string FIRST_NAME { get; set; }
        public string LAST_NAME { get; set; }

        public virtual ICollection<DB_CUSTOMER_USER> DB_CUSTOMER_USER { get; set; }
        public virtual ICollection<DB_USER_PHONES> DB_USER_PHONES { get; set; }
    }

 public partial class DB_CUSTOMER_USER
    {
        public int USER_ID { get; set; }
        public string CUSTOMER_MODULO { get; set; }
        public decimal ID { get; set; }

        public virtual DB_USER DB_USER { get; set; }
    }

public partial class DB_USER_PHONES
    {
        public string PHONE { get; set; }
        public int USER_ID { get; set; }

        public virtual DB_USER DB_USER { get; set; }
    }

And i made this viewmodel (i hope is correct).

    public class UserData
        {
            public int ID { get; set; }
            public string FIRST_NAME { get; set; }
            public string LAST_NAME { get; set; }

            public virtual ICollection<UserCustomer> UserCustomer { get; set; }
            public virtual ICollection<UserPhone> UserPhones { get; set; }
        }


    public class UserCustomer
        {
            public int USER_ID { get; set; }
            public string CUSTOMER_MODULO { get; set; }
            public decimal ID { get; set; }

            public virtual UserData UserData { get; set; }
        }

    public class UserPhone
        {
            public string PHONE { get; set; }
            public int USER_ID { get; set; }

            public virtual UserData UserData { get; set; }
        }

I need to send these data to view as a viewmodel and i this the best way would be use AutoMapper.

tereško
  • 58,060
  • 25
  • 98
  • 150
DiPix
  • 5,755
  • 15
  • 61
  • 108
  • What do you have with AutoMapper so far? Do you have a map from the collection types (e.g. `DB_CUSTOMER_USER` to `UserCustomer`)? – D Stanley Jun 14 '16 at 16:14
  • Well I never did before mapping with collection. I can map only single objects Eg. `Mapper.CreateMap(); PersonViewModel personViewModel = Mapper.Map(personModel);` So i don't know how to start with this. – DiPix Jun 14 '16 at 16:17
  • Add a map from `DB_CUSTOMER_USER` to `UserCustomer`, then configure the map from `PersonModel` to `PersonViewModel` to map the properties. AutoMapper will see that they're collections and copy them automatically. – D Stanley Jun 14 '16 at 16:22
  • `PersonalViewModel` - it was just an example. You mean `DB_CUSTOMER_USER` to `UserCustomer`? – DiPix Jun 14 '16 at 16:30
  • You can map the types in the collection, no need to map the xollection itself. Automapper will figure out how to convert collections of known types. Here is a working example: http://stackoverflow.com/questions/20635534/simple-automapper-example – meJustAndrew Jun 14 '16 at 16:44

0 Answers0