4
对我有这样的事情:我使用var apiEntity = Mapper.Map<DomainEntity, ApiEntity>(myDomainEntity);
AutoMapper设置属性设置为null目标对象
Mapper.Configuration.AllowNullCollections = true;
Mapper.CreateMap<DomainEntity, ApiEntity>().
ForSourceMember(e => e.OtherEntities, opt => opt.Ignore()).
ForSourceMember(e => e.AntherEntities, opt => opt.Ignore()).
ForMember(e => e.OtherEntitiesCount, opt => opt.MapFrom(src => src.OtherEntities.Count()));
Mapper.CreateMap<ApiEntity, DomainEntity>().
ForSourceMember(e => e.OtherEntitiesCount, opt => opt.Ignore()).
ForMember(e => e.OtherEntities, opt => opt.Ignore()).
ForMember(e => e.AnotherEntities, opt => opt.Ignore());
要获得从DomainEntity的ApiEntity:
public class DomainEntity
{
public string Name { get; set; }
public string Street { get; set; }
public IEnumerable<DomainOtherEntity> OtherEntities { get; set; }
public IEnumerable<DomainAnotherEntity> AnotherEntities { get; set; }
}
public class ApiEntity
{
public string Name { get; set; }
public string Street { get; set; }
public int OtherEntitiesCount { get; set; }
}
而继映射配置
要从ApiEntity中获取合并的DomainEntity,我正在使用var domainEntity = Mapper.Map(myApiEntity, myDomainEntity);
但是当使用这个时,属性OtherEntities
和AnotherEntities
被设置为null
- 即使它们在调用从myApiEntity
到myDomainEntity
的映射之前具有值。我怎样才能避免这个,所以他们真的合并而不只是更换值?
感谢您的任何帮助。
什么版本'AutoMapper'您使用的?你的方案在'2.2.1'上可以。 – Mightymuke 2013-04-21 02:29:38
我正在使用NuGet上的最新版本,它目前是2.2.1 – 2013-04-21 10:09:55