类型的异常“AutoMapper.AutoMapperMappingException”发生在AutoMapper.dll但在用户code没有处理 [英] An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code
问题描述
不知怎的,我的code不工作了(它确实具有完全相同的code工作之前)。这就是问题:
的code
我想一定对象的ViewModels这个code图:
配置:
Mapper.CreateMap< BookcaseItem,FoundBookcaseItemViewModel>()
.ForMember(X => x.Title,选择=> opt.MapFrom(SRC => src.Book.Title))
.ForMember(X => x.Authors,停用= GT; opt.MapFrom(SRC => src.Book.Authors.Select(X => x.Name).Aggregate((I,J)=> I +,+ J)))
.ForMember(X => x.Identifiers,选择=> opt.MapFrom(SRC =>(string.IsNullOrEmpty(src.Book.Isbn10)(ISBN10:?+ src.Book.Isbn10 +\\ r \\ N)的String.Empty)+
(string.IsNullOrEmpty(src.Book.Isbn13)(哲学,!?+ src.Book.Isbn13)的String.Empty)))
.ForMember(X => x.Pages,选择=> opt.MapFrom(SRC => src.Book.Pages))
.ForMember(X => x.ImageUri,选择=> opt.MapFrom(SRC => src.Book.ThumbnailUriSmall));
用法:
公众的ActionResult指数()
{
字符串facebookId = _accountService.GetLoggedInUserFacebookId(); IEnumerable的< BookcaseItem>项目= _bookcaseItemService.GetBookcaseItemsForUser(facebookId);
IEnumerable的< FoundBookcaseItemViewModel>的ViewModels = items.Select(Mapper.Map< BookcaseItem,FoundBookcaseItemViewModel>); 返回PartialView(的ViewModels);
}
错误
这将导致以下错误:
类型的异常AutoMapper.AutoMapperMappingException发生在AutoMapper.dll但在用户code没有处理
块引用>调试数据
首先我保证不存在任何配置错误,通过调用:
Mapper.AssertConfigurationIsValid();
我已经设置断点都在我的code和尝试调试它,但我无法理解它。在项目集合填充数据(由实体框架生成的代理类),但在'的ViewModels收集充满了奇怪的数据。它有说这是一个消息:
映射类型:
BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB
- >字符串System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB
- > System.String
目标路径:FoundBookcaseItemViewModel.Authors
来源值:
System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB
块引用>再有就是,说一个堆栈跟踪属性:
在System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
在System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
块引用>呵呵,终于有一个名为语境具有下列数据的属性:
谁能解释一下是怎么回事,为什么我的code工作不下去了?我做了一些更改,以我的解决方案最近,但我滚回去用Git的,所以他们不应该对code任何影响。
我的设置
- 的Visual Studio 12 RC
- ASP.NET MVC 4
- 的.NET Framework 4.0(我有4.5,但导致了太多的错误,所以我使用Git回滚到4.0版本)
- 实体框架5.0 RC
- AutoMapper 2.1.267
实体和视图模型
我不知道这是否是相关的,但这里是该映射的源类:
公共类BookcaseItem:实体
{
公众的Guid标识{搞定;组; }
公共BOOL IsRenting {搞定;组; }
公共BOOL IsSwapping {搞定;组; }
公共BOOL IsSelling {搞定;组; }
公共小数RentingPrice {搞定;组; }
公共小数SellingPrice {搞定;组; }
公共字符串货币{搞定;组; }
公共BOOL IsAvailable {搞定;组; }
公共虚拟的Guid BOOKID {搞定;组; }
公共虚拟的Guid用户Id {搞定;组; } 公共虚拟书书{搞定;组; }
公共虚拟用户用户{搞定;组; } 公共BookcaseItem()
{
IsAvailable = TRUE;
货币=USD;
}
}这是该映射的目标类:
公共类FoundBookcaseItemViewModel
{
公众的Guid标识{搞定;组; }
公共BOOL IsRenting {搞定;组; }
公共BOOL IsSwapping {搞定;组; }
公共BOOL IsSelling {搞定;组; }
公共小数RentingPrice {搞定;组; }
公共小数SellingPrice {搞定;组; }
公共字符串名称{搞定;组; }
公共字符串作者{搞定;组; }
公共字符串标识符{搞定;组; }
公众诠释页{搞定;组; }
公共字符串ImageUri {搞定;组; }
}
解决方案好像有一个与映射Authors属性的一个问题。如果作者顺序为null或空此汇总调用会抛出异常。
.ForMember(X => x.Authors,
选择=> opt.MapFrom(SRC => src.Book.Authors.Select(X => x.Name).Aggregate((I,J)= I标记+,+ J)))Somehow my code doesn't work any more (it did work before with the exact same code). This is the problem:
The code
I'm trying to map some objects to ViewModels with this code:
Configuration:
Mapper.CreateMap<BookcaseItem, FoundBookcaseItemViewModel>() .ForMember(x => x.Title, opt => opt.MapFrom(src => src.Book.Title)) .ForMember(x => x.Authors, opt => opt.MapFrom(src => src.Book.Authors.Select(x => x.Name).Aggregate((i, j) => i + ", " + j))) .ForMember(x => x.Identifiers, opt => opt.MapFrom(src => (!string.IsNullOrEmpty(src.Book.Isbn10) ? ("ISBN10: " + src.Book.Isbn10 + "\r\n") : string.Empty) + (!string.IsNullOrEmpty(src.Book.Isbn13) ? ("ISBN13: " + src.Book.Isbn13) : string.Empty))) .ForMember(x => x.Pages, opt => opt.MapFrom(src => src.Book.Pages)) .ForMember(x => x.ImageUri, opt => opt.MapFrom(src => src.Book.ThumbnailUriSmall));
The usage:
public ActionResult Index() { string facebookId = _accountService.GetLoggedInUserFacebookId(); IEnumerable<BookcaseItem> items = _bookcaseItemService.GetBookcaseItemsForUser(facebookId); IEnumerable<FoundBookcaseItemViewModel> viewModels = items.Select(Mapper.Map<BookcaseItem, FoundBookcaseItemViewModel>); return PartialView(viewModels); }
The error
This results in the following error:
An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code
The debug data
First of all I ensure that there are no configuration errors by calling:
Mapper.AssertConfigurationIsValid();
I've set breakpoints all over my code and try to debug it, but I can't make sense of it. The 'items' collection is filled with data (proxy classes generated by Entity Framework), but the the 'viewModels' collection is filled with strange data. It has a 'message' that says this:
Mapping types: BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB -> String System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB -> System.String
Destination path: FoundBookcaseItemViewModel.Authors
Source value: System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB
And then there's a stacktrace property that says:
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
Oh and finally there's another property called 'context' with the following data:
Can anyone explain what's going on here and why my code isn't working any longer? I did a couple of changes to my solution recently, but I've rolled them back by Git, so they shouldn't have any effect on the code.
My setup
- Visual Studio 12 RC
- ASP.NET MVC 4
- .NET Framework 4.0 (I had 4.5 but that caused too many errors, so I rolled back with Git to version 4.0)
- Entity Framework 5.0 RC
- AutoMapper 2.1.267
The entity and ViewModel
I don't know if it's relevant, but here is the source class for the mapping:
public class BookcaseItem : Entity { public Guid Id { get; set; } public bool IsRenting { get; set; } public bool IsSwapping { get; set; } public bool IsSelling { get; set; } public decimal RentingPrice { get; set; } public decimal SellingPrice { get; set; } public string Currency { get; set; } public bool IsAvailable { get; set; } public virtual Guid BookId { get; set; } public virtual Guid UserId { get; set; } public virtual Book Book { get; set; } public virtual User User { get; set; } public BookcaseItem() { IsAvailable = true; Currency = "USD"; } }
And this is the destination class for the mapping:
public class FoundBookcaseItemViewModel { public Guid Id { get; set; } public bool IsRenting { get; set; } public bool IsSwapping { get; set; } public bool IsSelling { get; set; } public decimal RentingPrice { get; set; } public decimal SellingPrice { get; set; } public string Title { get; set; } public string Authors { get; set; } public string Identifiers { get; set; } public int Pages { get; set; } public string ImageUri { get; set; } }
解决方案It seems like there is a problem with mapping Authors property. This Aggregate call will throw an exception if the Authors sequence is null or empty.
.ForMember(x => x.Authors, opt => opt.MapFrom(src => src.Book.Authors.Select(x => x.Name).Aggregate((i, j) => i + ", " + j)))
这篇关于类型的异常“AutoMapper.AutoMapperMappingException”发生在AutoMapper.dll但在用户code没有处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!