开发者

AutoMapper Problem - List won't Map

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.开发者_StackOverflow中文版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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜