在最短的一到两周内,我们的代码中出现了一个奇怪的错误。我试图找出映射失败的根本原因。最内在的异常本身令人费解:Type 'System.String' does not have a default constructor
我不明白异常告诉我的是什么。你能解释一下发生了什么,也许我可以解决这个错误吗?
映射器在通用方法中使用:
public TEntity UpdateWithHistory<TEntity>(TEntity entity, int? entityID, int? interviewID)
where TEntity : class
{
var snapshot = _interviewContext.Find<TEntity>(entityID);
// This is call that fails
var history = Mapper.Map<TEntity, TEntity>(snapshot);
_interviewHistory.Set<TEntity>().Add(history);
MarkModified(entity);
return Mapper.Map(entity, snapshot);
}
在上面的代码中,快照不是null。完整的例外:
AutoMapper.AutoMapperMappingException:
Trying to map Recog.Web.Models.InterviewComment to Recog.Web.Models.InterviewComment.
Using mapping configuration for Recog.Web.Models.InterviewComment to Recog.Web.Models.InterviewComment
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
---> AutoMapper.AutoMapperMappingException: Trying to map System.String to System.String.
Using mapping configuration for System.String to System.String
Destination property: Comment
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
---> AutoMapper.AutoMapperMappingException: Trying to map System.String to System.String.
Using mapping configuration for System.String to System.String
Destination property: Comment
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.
---> System.ArgumentException: Type 'System.String' does not have a default constructor
at System.Linq.Expressions.Expression.New(Type type)
at AutoMapper.DelegateFactory.CreateCtor(Type type)
at AutoMapper.Mappers.ObjectCreator.CreateObject(Type type)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.CreateObject(ResolutionContext context)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.NewObjectPropertyMapMappingStrategy.GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
--- End of inner exception stack trace ---
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap)
--- End
提到的Comment类:
public class InterviewComment
{
[Key]
public int? InterviewCommentID { get; set; }
[ForeignKey("Interview")]
public int? InterviewID { get; set; }
[CodeType(CodeTypeEnum.CommentSection)]
public int? CommentSectionCodeID { get; set; }
[CodeType(CodeTypeEnum.CommentSource)]
public int? CommentSourceCodeID { get; set; }
[Display(Name = "Comment")]
[StringLength(int.MaxValue)]
public string Comment { get; set; }
[Include]
[Association("Interview_1-*_InterviewComment", "InterviewID", "InterviewID", IsForeignKey = true)]
public virtual Interview Interview { get; set; }
[ReadOnly(true)]
[ForeignKey("ModifiedByUser")]
public virtual ApplicationUser ApplicationUser { get; set; }
[ReadOnly(true)]
public string UserName
{
get { return ApplicationUser != null ? ApplicationUser.GetDisplayName() : null; }
}
[ReadOnly(true)]
public int CreatedByUser { get; set; }
[ReadOnly(true)]
public DateTime CreatedDateTime { get; set; }
[ReadOnly(true)]
public int ModifiedByUser { get; set; }
[ReadOnly(true)]
public DateTime ModifiedDateTime { get; set; }
}
我仍在审核最近的提交,以确定导致此问题的更改。任何对异常的见解都将非常感激。
答案 0 :(得分:6)
错误的根本原因在于未共享的代码。我们有一个约定,为通过反射发现的特定类型配置映射。我们的算法错误地为string
创建了一个地图,取代了AutoMapper提供的默认映射。
如果您看到错误Type 'System.String' does not have a default constructor
,请确认您的代码未为string
创建地图。
答案 1 :(得分:2)
String没有默认构造函数,因为字符串是不可变的,所以无论如何都不会有用。
要缩小您的问题范围,请向该方法提供哪些数据。您要映射的数据是什么。您要映射的项目中的Comment属性的值是多少?
答案 2 :(得分:2)
我有同样的错误。 我想将对象数组映射到对象数组
此生成的错误
AutoMapper.AutoMapperMappingException:输入'TargetObjectType'
没有默认构造函数:
AutoMapper.Mapper.CreateMap<SourceObjectType[], TargetObjectType[]>();
TargetObject = AutoMapper.Mapper.Map<SourceObjectType[], TargetObjectType[]>(SourceObject);
当我为映射设置正确的类型时,错误消失了。
AutoMapper.Mapper.CreateMap<SourceObjectType, TargetObjectType>();