AutoMapper.AutoMapperMappingException: Type 'System.String' does not have a default constructor

A strange error began to appear in our code in a week or two. I am trying to determine the root cause of the display failure. The innermost exception is perplexing: Type 'System.String' does not have a default constructor

I don’t understand what the exception tells me. Can you explain what happened, and maybe, how can I solve this error?

The converter is used in the general method:

 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); } 

In the above code, the snapshot is NOT zero. Full exception:

 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 

The Comment class mentioned:

 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; } } 

I am still looking through recent commits to determine the change that causes this. Any understanding of the exception would be greatly appreciated.

+4
source share
3 answers

The root cause of the error was code that was not shared. We have an agreement in which mappings are configured for certain types detected through reflection. Our algorithm incorrectly created a map for string , replacing the default mapping provided by AutoMapper.

If you ever see a Type 'System.String' does not have a default constructor error, confirm that your code does not create a map for string .

+6
source

The string does not have a default constructor that would not be useful since the string is immutable.

To narrow down your problem, what data is provided to the method. What data are you trying to match. What is the value of the Comment property in the element you are trying to match?

+2
source

I had the same error. I wanted to map an array of objects to an array of objects

This generated error

AutoMapper.AutoMapperMappingException: Type 'TargetObjectType'

does not have a default constructor:

 AutoMapper.Mapper.CreateMap<SourceObjectType[], TargetObjectType[]>(); TargetObject = AutoMapper.Mapper.Map<SourceObjectType[], TargetObjectType[]>(SourceObject); 

When I set the types to match correctly, then the error disappears.

 AutoMapper.Mapper.CreateMap<SourceObjectType, TargetObjectType>(); 
+2
source

All Articles