AutoMapper.dll中出现“AutoMapper.AutoMapperMappingException”类型的例外,但未在用户代码中处理
不知何故,我的代码不再起作用(它之前使用完全相同的代码工作)。 这就是问题:
代码
我正在尝试使用以下代码将一些对象映射到ViewModels:
组态:
Mapper.CreateMap() .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));
用法:
public ActionResult Index() { string facebookId = _accountService.GetLoggedInUserFacebookId(); IEnumerable items = _bookcaseItemService.GetBookcaseItemsForUser(facebookId); IEnumerable viewModels = items.Select(Mapper.Map); return PartialView(viewModels); }
错误
这会导致以下错误:
AutoMapper.dll中出现“AutoMapper.AutoMapperMappingException”类型的例外,但未在用户代码中处理
调试数据
首先,我通过调用以下方法确保没有配置错误:
Mapper.AssertConfigurationIsValid();
我在我的代码中设置断点并尝试调试它,但我无法理解它。 ‘items’集合中填充了数据(Entity Framework生成的代理类),但’viewModels’集合中充满了奇怪的数据。 它有一条“消息”,上面写着:
映射类型:BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB – > String System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB – > System.String
目标路径:FoundBookcaseItemViewModel.Authors
来源价值:System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB
然后有一个stacktrace属性说:
在System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
在System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
哦,最后还有另一个名为’context’的属性,其中包含以下数据:
任何人都可以解释这里发生了什么,为什么我的代码不再工作? 我最近对我的解决方案进行了一些更改,但是我已经将它们用Git回滚,所以它们不会对代码产生任何影响。
我的设置
- Visual Studio 12 RC
- ASP.NET MVC 4
- .NET Framework 4.0(我有4.5但是导致了太多错误,所以我用Git回滚到版本4.0)
- entity framework5.0 RC
- AutoMapper 2.1.267
实体和ViewModel
我不知道它是否相关,但这里是映射的源类:
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"; } }
这是映射的目标类:
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; } }
似乎映射Authors属性存在问题。 如果Authors序列为null或为空,则此Aggregate调用将引发exception。
.ForMember(x => x.Authors, opt => opt.MapFrom(src => src.Book.Authors.Select(x => x.Name).Aggregate((i, j) => i + ", " + j)))