不知何故,我的代码不再起作用(它之前确实使用完全相同的代码起作用)。这就是问题:
编码
我正在尝试使用以下代码将一些对象映射到 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);
}
错误
这会导致以下错误:
AutoMapper.dll 中出现“AutoMapper.AutoMapperMappingException”类型的异常,但未在用户代码中处理
调试数据
首先,我通过调用确保没有配置错误:
Mapper.AssertConfigurationIsValid();
我已经在我的代码中设置了断点并尝试调试它,但我无法理解它。“items”集合充满了数据(实体框架生成的代理类),但“viewModels”集合充满了奇怪的数据。它有一条“消息”,上面写着:
映射类型:BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB -> String System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D35727DFBB -> System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D35727DFB5
目标路径:FoundBookcaseItemViewModel.Authors
源值:System.Data.Entity.DynamicProxies.BookcaseItem_B9B52593B2659AC05C47AB2A6E0F7AEA9989CC34D3527DF5B6AA988ED57166FB
然后有一个堆栈跟踪属性说:
在 System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
在 System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
哦,最后还有一个名为“context”的属性,其中包含以下数据:
谁能解释这里发生了什么以及为什么我的代码不再工作?我最近对我的解决方案进行了一些更改,但我已通过 Git 将它们回滚,因此它们不应该对代码产生任何影响。
我的设置
- 视觉工作室 12 RC
- ASP.NET MVC 4
- .NET Framework 4.0(我有 4.5,但导致错误太多,所以我使用 Git 回滚到 4.0 版)
- 实体框架 5.0 RC
- 自动映射器 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; }
}