不知何故,我的代码不再起作用(它以前使用完全相同的代码确实起作用)。这是问题所在:
代码
我正在尝试使用以下代码将一些对象映射到 ViewModel:
配置:
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));
用法:
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);
}
错误
这会导致以下错误:
An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code
调试数据
首先我通过调用确保没有配置错误:
Mapper.AssertConfigurationIsValid();
我在我的代码中设置了断点并尝试调试它,但我无法理解它。 'items' 集合中充满了数据(由 Entity Framework 生成的代理类),但 '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
然后有一个堆栈跟踪属性说:
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
哦,最后还有另一个名为“上下文”的属性,其中包含以下数据:
谁能解释这里发生了什么以及为什么我的代码不再工作?我最近对我的解决方案做了一些更改,但我已经通过 Git 回滚了它们,因此它们应该不会对代码产生任何影响。
我的设置
- Visual Studio 12 RC
- ASP.NET MVC 4
- .NET Framework 4.0(我有 4.5,但错误太多,所以我用 Git 回滚到 4.0 版)
- Entity Framework 5.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 或空,此聚合调用将抛出异常。
.ForMember(x => x.Authors,
opt => opt.MapFrom(src => src.Book.Authors.Select(x => x.Name).Aggregate((i, j) => i + ", " + j)))
关于c# - AutoMapper.dll中出现 'AutoMapper.AutoMapperMappingException'类型的异常,但用户代码中没有处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11072877/