C# AutoMapper.dll 中发生了“AutoMapper.AutoMapperMappingException”类型的异常,但未在用户代码中处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11072877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code
提问by Leon Cullens
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:
我正在尝试使用以下代码将一些对象映射到 ViewModel:
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
AutoMapper.dll 中发生了“AutoMapper.AutoMapperMappingException”类型的异常,但未在用户代码中处理
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:
我在我的代码中设置了断点并尝试调试它,但我无法理解它。'items' 集合充满了数据(由实体框架生成的代理类),但 'viewModels' 集合充满了奇怪的数据。它有一个“消息”,上面写着:
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
映射类型:BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB -> 字符串 System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B264ED580000000ACC564ED57593B264ED57593B264ED571678D7A8D3528DF5B6AA988ED57166FB
目标路径:FoundBookcaseItemViewModel.Authors
源值: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()
在 System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
在 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.
谁能解释这里发生了什么以及为什么我的代码不再起作用?我最近对我的解决方案做了一些更改,但我已经通过 Git 回滚了它们,所以它们不应该对代码产生任何影响。
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
- Visual Studio 12 RC
- ASP.NET MVC 4
- .NET Framework 4.0(我有 4.5,但这导致了太多错误,所以我用 Git 回滚到 4.0 版)
- 实体框架 5.0 RC
- 自动映射器 2.1.267
The entity and ViewModel
实体和 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; }
}
采纳答案by Ufuk Hac?o?ullar?
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.
映射 Authors 属性似乎存在问题。如果 Authors 序列为 null 或为空,则此 Aggregate 调用将引发异常。
.ForMember(x => x.Authors,
opt => opt.MapFrom(src => src.Book.Authors.Select(x => x.Name).Aggregate((i, j) => i + ", " + j)))

