0

How can I use automapper to map ICollection property in my model class.

Here is example:

 Mapper.CreateMap<Planiranje.Planning.Data.User, Model.Users>()
                .ForMember(dst => dst.UserId, opts => opts.MapFrom(src => src.UserId))
                .ForMember(dst => dst.Username, opts => opts.MapFrom(src => src.Username))
                .ForMember(dst => dst.Firstname, opts => opts.MapFrom(src => src.Firstname))
                .ForMember(dst => dst.Lastname, opts => opts.MapFrom(src => src.Lastname))
                .ForMember(dst => dst.Email, opts => opts.MapFrom(src => src.Email))
                .ForMember(dst => dst.Segment, opts => opts.MapFrom(src => src.Segment))
                .ForMember(dst => dst.Groups, opts => opts.MapFrom(src => src.Groups));

Where groups is ofType ICollection.

With this mapping I'm getting the following error

The following property on System.Text.RegularExpressions.Group cannot be mapped: 
    Groups
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type System.Text.RegularExpressions.Group.
Context:
    Mapping to property Groups from Planiranje.Planning.Data.Group to System.Text.RegularExpressions.Group
    Mapping to property Groups from System.Collections.Generic.ICollection`1[[Planiranje.Planning.Data.Group, Planiranje, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.ICollection`1[[System.Text.RegularExpressions.Group, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
    Mapping from type Planiranje.Planning.Data.User to Planiranje.Model.Users
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.

After I get rid of Syste.Text.RegularExpression I got the following error message

The following property on Planiranje.Model.Groups cannot be mapped: 
    Groups
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Planiranje.Model.Groups.
Context:
    Mapping to property Groups from Planiranje.Planning.Data.Group to Planiranje.Model.Groups
    Mapping to property Groups from System.Collections.Generic.ICollection`1[[Planiranje.Planning.Data.Group, Planiranje, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.ICollection`1[[Planiranje.Model.Groups, Planiranje, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
    Mapping from type Planiranje.Planning.Data.User to Planiranje.Model.Users
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.

Also, here are my Source/Destination classes Source classes

namespace Planiranje.Planning.Data
{
    using System;
    using System.Collections.Generic;

    public partial class User
    {
        public User()
        {
            this.Groups = new HashSet<Group>();
        }

        public int UserId { get; set; }
        public string Username { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Email { get; set; }
        public string Segment { get; set; }

        public virtual ICollection<Group> Groups { get; set; }
    }
}

namespace Planiranje.Planning.Data
{
    using System;
    using System.Collections.Generic;

    public partial class Group
    {
        public Group()
        {
            this.Users = new HashSet<User>();
        }

        public int GroupId { get; set; }
        public string GroupName { get; set; }

        public virtual ICollection<User> Users { get; set; }
    }
}

and destination classes :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;

namespace Planiranje.Model
{
    public class Users : ModelBase,IEntity
    {
        private int _userId;
        public int UserId 
        {
            get
            {
                return _userId;
            }
            set
            {
                _userId = value;
                RaisePropertyChanged("UserId");
            }
        }
        private string _username;
        public string Username 
        {
            get
            {
                return _username;
            }
            set
            {
                _username = value;
                RaisePropertyChanged("Username");
            }
        }
        private string _firstname;
        public string Firstname
        {
            get
            {
                return _firstname;
            }
            set
            {
                _firstname = value;
                RaisePropertyChanged("Firstname");
            }
        }
        private string _lastname;
        public string Lastname 
        {
            get
            {
                return _lastname;
            }
            set
            {
                _lastname = value;
                RaisePropertyChanged("Lastname");
            }
        }
        private string _email;
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
                RaisePropertyChanged("Email");
            }
        }
        private string _segment;
        public string Segment 
        {
            get
            {
                return _segment;
            }
            set
            {
                _segment = value;
                RaisePropertyChanged("Segment");
            }

        }

        private IList<Groups> _groups;
        public virtual IList<Groups> Groups
        {
            get
            {
                return _groups;
            }
            set
            {
                _groups = value;
                RaisePropertyChanged("Groups");
            }
        }

        public ModelEntityState ModelEntityState { get; set;}

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;

namespace Planiranje.Model
{
    public class Groups :ModelBase, IEntity
    {
        private int _groupId;
        public int GroupId 
        {
            get
            {
                return _groupId;
            }
            set
            {
                _groupId = value;
                RaisePropertyChanged("UserId");
            }
        }
        private string _groupname;
        public string GroupName 
        {
            get
            {
                return _groupname;
            }
            set
            {
                _groupname = value;
                RaisePropertyChanged("GroupName");
            }
        }

        private ICollection<Users> _users;
        public virtual ICollection<Users> Users 
        {
            get
            {
                return _users;
            }
            set
            {
                _users = value;
                RaisePropertyChanged("Users");
            }
        }

        public ModelEntityState ModelEntityState { get; set; }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Almir
  • 11
  • 2
  • Just one comment - all those MapFroms are completely pointless. Don't configurations for mapping properties of the exact same name. That's the "Auto" of "AutoMapper". Also please post your source/destination types. – Jimmy Bogard May 05 '15 at 13:28

2 Answers2

0

AutoMapper will Map the ICollection group just fine. That is not your problem. The problem described by the Exception is:

  • Your source property Group is of type Planiranje.Planning.Data.Group
  • But your destination property Group is of type System.Text.RegularExpressions.Group

I guess that the destination property type is the mistake.

I guess further that your file Model.Users.cs has got a using System.Text.RegularExpressions at the top of it.

If so, your best fix might be to add

using Group=Planiranje.Planning.Data.Group;

to the top of Model.Users.cs

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61
  • Hi Chris,you were right about using System.Text.RegularExpressions. I removed the same, but still I get the same error message as I stated. Any other thoughts. Thanks – Almir May 05 '15 at 12:55
  • Can you edit your question to show the new error message after you've got rid of the System.Text.RegularExpressions ? If it's still the same error message after recompiling then Model.Users.Group is still of type System.Text.RegularExpressions.Group – Chris F Carroll May 05 '15 at 12:59
0

I think the problem is that as well as your mapping

Mapper.CreateMap<Planiranje.Planning.Data.User, Model.Users>()

you must also define a mapping

Mapper.CreateMap<Planiranje.Planning.Data.Group,Planiranje.Model.Groups>()

And you have to carry on doing this recursively for all the types that your classes depend on.

I have seen AutoMapper configurations grow very big like this when there are whole families of classes.

Chris F Carroll
  • 11,146
  • 3
  • 53
  • 61