我有以下两个类(为简洁起见,省略了许多属性)。
服务层 POCO:
public class TicketFlag
{
public ContactKey ContactKey;
}
LINQ to SQL 生成 POCO:
public class TicketFlag
{
public string ContactKey;
}
当尝试使用 AutoMapper 在服务调用-> 数据库保存时在这两者之间进行映射时,出现以下异常:
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
---> System.ArgumentException: Type 'ContactKey' does not have a default constructor
ContactKey 故意没有默认构造函数。基本上,它需要一个字符串和一个对象列表,并且可以对其自身进行序列化/反序列化。
我尝试过创建一个映射函数(它是逆函数),如下所示:
Mapper.CreateMap<string, ContactKey>().ConvertUsing(s => ContactKeySerializer.Serialize(s));
但我仍然收到投诉,因为 ContactKey 没有默认构造函数。
有没有办法让 AutoMapper 不使用默认构造函数来进行属性映射?实际上,仅在 ContactKey 上映射属性是不够的——我需要让它调用构造函数,或者从我的 ContactKeySerializer 类中吐出。