- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在最近一两周内,我们的代码中开始出现一个奇怪的错误。我正在尝试确定映射失败的根本原因。最内部的异常本身就令人费解: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/
当我收到此异常时,是否可以通过某种方式从automapper中获取更多详细信息: AutoMapper.AutoMapperMappingException 通常,它会告诉我这两种映射类型,但不会告诉
当我在我的代码中实现 IReadOnlyList 时,我的单元测试抛出了一个 AutoMapperMappingException。 通过深入研究文章和文档,我猜测 AutoMapper 需要对 re
我猜测除了异常之外的所有信息都是额外的并且几乎不相关(并且在这里是为了完整性)。问题是:根据下面的异常,到底遇到了什么问题? 我无法理解异常消息。据我所知,将 BsonDocument 映射到 Chi
public class ClientViewModel { [Required(ErrorMessage = "The Client Code field is requir
Automapper 出现特殊错误 错误信息: Mapping types: TransportOffer -> TransportOfferDto Model.TransportOffer -> D
_quatationRepository = new QuatationRepository(); IList quatationObj = _quatationRepository.GetAll(q
我有类似 this. 的问题但是这个答案对我不起作用。 我正在做一个小项目。我有两个域模型(Post,Source): public class Post { public Post()
在最近一两周内,我们的代码中开始出现一个奇怪的错误。我正在尝试确定映射失败的根本原因。最内部的异常本身就令人费解:Type 'System.String' does not have a defaul
我在 .NET CORE 应用程序中使用 AutoMapper。我在 ConfigureServices.cs 方法文件中的 Startup.cs 中配置了 AutoMapper,如下所示: publ
我在我的 ASP.NET Core 2.1 Web 应用程序中使用 AutoMapper 7.0.1 和 AutoMapper.Extensions.Microsoft.DependencyInjec
我的实体 public partial class Users { public Users() { sexs = new HashSet(); } [
我是一名优秀的程序员,十分优秀!