gpt4 book ai didi

c# - AutoMapper.AutoMapperMappingException : Type 'System.String' does not have a default constructor 问题

转载 作者:太空狗 更新时间:2023-10-29 22:28:24 25 4
gpt4 key购买 nike

在最近一两周内,我们的代码中开始出现一个奇怪的错误。我正在尝试确定映射失败的根本原因。最内部的异常本身就令人费解: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);
}

在上面的代码中,snapshot 不为空。完整的异常:

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

我仍在审查最近的提交以确定导致此问题的更改。非常感谢任何对该异常的洞察。

最佳答案

错误的根本原因在于未共享的代码。我们有一个约定,为通过反射发现的特定类型配置映射。我们的算法错误地为 string 创建了一个映射,替换了 AutoMapper 提供的默认映射。

如果您看到错误 Type 'System.String' does not have a default constructor,请确认您的代码没有为 string 创建映射。

关于c# - AutoMapper.AutoMapperMappingException : Type 'System.String' does not have a default constructor 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6254639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com