我在使用AutoMapper将数据传输对象映射到数据库实体模型时遇到了些问题。该实体具有一些自定义的数组类型属性,派生自IEnumerable,并且这些属性没有setter,但有一个名为
以下是我正在处理的关键类的缩减版本。(从实体到DTO的映射工作正常,但我需要它也能在相反的方向上工作。)
感谢您能提供的任何帮助!
SetFromString()
的方法可用。我无法正确配置我的映射来使用它。AutoMapper支持这种操作吗?如果有人能指点我正确的方向,我将不胜感激。以下是我正在处理的关键类的缩减版本。(从实体到DTO的映射工作正常,但我需要它也能在相反的方向上工作。)
// The database entity
public class ContactEntity
{
public CustomArray<String> CustomerNumbers { get; }
}
// The data transfer object
public class ContactDto
{
public List<String> CustomerNumbers { get; set; }
}
// CustomArray definition
public abstract class CustomArray<DataType> : IEnumerable<DataType>, IDisposable
{
protected CustomArray();
public abstract void SetFromString(string Value);
}
我的映射配置文件还是很基本的,因为我无法理解正确的ForMember
语法。
public class ContactMappingProfile : Profile
{
public ContactMappingProfile()
{
// This map works fine
CreateMap<ContactEntity, ContactDto>();
// Map from DTO to Entity
CreateMap<ContactDto, ContactEntity>()
.ForMember(dest => dest.CustomerNumbers,
opt => opt.ResolveUsing(src => src.CustomerNumbers));
}
}
感谢您能提供的任何帮助!
List<String>
转换为SetFromString
方法所需的string Value
? - Ivan Stoevstring.Join(",", src.CustomerNumbers);
。我缺少的部分是如何将该值传递给dest.CustomerNumbers.SetFromString();
。 - wags