AutoMapper.AutoMapperMappingException: Type De Système.Chaîne de " ne pas avoir un constructeur par défaut

Une erreur étrange a commencé à se manifester dans notre code dans la peur de semaine ou deux. Je suis en train d'essayer d'identifier la cause racine de la cartographie de l'échec. La plus intérieure de l'exception elle-même est perplexe: Type 'System.String' does not have a default constructor

Je ne comprends pas ce que l'exception est de me dire. Pouvez-vous expliquer ce qui s'est passé et peut-être comment je pourrais résoudre ce bug?

Le mappeur est utilisé à l'intérieur d'une méthode générique:

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

Dans le code ci-dessus, l'instantané n'est PAS nulle. La pleine 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

Le Commentaire de la classe qui est mentionné:

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

Je suis encore à l'examen des récentes s'engage à identifier le changement qui est à l'origine. Aucune information sur l'exception serait grandement apprécié.

Question stupide: est-ce AutoMapper de Mapper.AssertConfigurationIsValid() vous dire quelque chose?
Bonne idée et je n'avais pas pensé à l'essayer. Cependant, la config est bien valide.

OriginalL'auteur Ed Chapel | 2011-06-06