Search code examples
c#many-to-manyautomapper

Automapper Many to Many


I've been trying to get this working for too long. I've looked at every automapper question I could find, but still no luck.

How do I configure Automapper so that it will properly map my many to many properties between my business entity and my database model?
I'm using a repository pattern using a DB first data model.

I have these entity objects:

 public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public DateTime CreateDate { get { return DateTime.Now; } private set { } }
    public List<Department> Departments { get; set; }
    public List<Company> Companies { get; set; }
}

  public class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreateDate { get { return DateTime.Now; } private set { } }
}

public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreateDate { get { return DateTime.Now; } private set { } }
}

I need to be able to update these. I need to map them to the db object so I can update the user.

 public partial class User{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public Nullable<System.DateTime> CreateDate { get; set; }


    public virtual ICollection<UsersCompany> UsersCompanies { get; set; }

    public virtual ICollection<UsersDepartment> UsersDepartments { get; set; }
}

public partial class UsersCompany
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int CompanyId { get; set; }

    public virtual Company Company { get; set; }
    public virtual User User { get; set; }
}

public partial class UsersDepartment
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int DepartmentId { get; set; }

    public virtual Department Department { get; set; }
    public virtual User User { get; set; }
}

I have a method in which I pass the entity and attempt to map it to the db model. This is what I have right now after about 100 different attempts to get automapper to politely map my join tables...with no luck.

private DBUser ToDataModel(User user)
    {

        var config = new MapperConfiguration(cfg =>
        {

            cfg.CreateMap<User, DBUser>()
            .ForMember(dest => dest.UsersDepartments, opt => opt.MapFrom(x => x.Departments));


            cfg.CreateMap<User, DBUsersDepartment>()
                .ForMember(x => x.User, y => y.MapFrom(z => z));


            cfg.CreateMap<Department, DBUsersDepartment>();


        });
        IMapper mapper = config.CreateMapper();

        return mapper.Map<DBUser>(user);
    }

This is what I see after it 'maps' (notice no user information has been mapped):

results


Solution

  • For anyone interested. I ended up going a different route, where I update each collection separately, then update the base user class. ...now off to the next issue.