tags:

views:

50

answers:

1

I have the following class:

public class Account
{
    public int AccountID { get; set; }
    public Enterprise Enterprise { get; set; }
    public List<User> UserList { get; set; }
}

And I have the following method fragment:

Entities.Account accountDto = new Entities.Account();
DAL.Entities.Account account;

Mapper.CreateMap<DAL.Entities.Account, Entities.Account>();
Mapper.CreateMap<DAL.Entities.User, Entities.User>();

account = DAL.Account.GetByPrimaryKey(this.Database, primaryKey, withChildren);

Mapper.Map(account,accountDto);
return accountDto;

When the method is called, the Account class gets mapped correctly but the list of users in the Account class does not (it is NULL). There are four User entities in the List that should get mapped. Could someone tell me what might be wrong?

A: 

Try not passing in the accountDto, and let AutoMapper create it for you. When you map to an existing destination object, AutoMapper makes a few assumptions, that you won't have any already-null destination collections for one. Instead, do:

var accountDto = Mapper.Map<DAL.Entities.Account, Entities.Account>(account);

The last thing you should check is that your configuration is valid, so you can try:

Mapper.AssertConfigurationIsValid();

After those CreateMap calls. This checks to make sure everything lines up properly on the destination type side of things.

Jimmy Bogard

related questions