2

我有以下课程:

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

我有以下方法片段:

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;

调用该方法时,Account 类被正确映射,但 Account 类中的用户列表没有(它为 NULL)。列表中有四个用户实体应该被映射。有人可以告诉我可能出了什么问题吗?

4

1 回答 1

3

尽量不要传入 accountDto,让 AutoMapper 为你创建。当您映射到现有目标对象时,AutoMapper 会做出一些假设,即您不会有任何已经为空的目标集合。相反,请执行以下操作:

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

您应该检查的最后一件事是您的配置是否有效,因此您可以尝试:

Mapper.AssertConfigurationIsValid();

在那些 CreateMap 调用之后。这会检查以确保所有东西在目标类型方面都正确排列。

于 2010-03-15T12:21:51.380 回答