Leo*_*ens 11 c# debugging asp.net-mvc automapper entity-framework-5
不知何故,我的代码不再起作用(它之前使用完全相同的代码工作).这就是问题:
代码
我正在尝试使用以下代码将一些对象映射到ViewModels:
组态:
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));
Run Code Online (Sandbox Code Playgroud)
用法:
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);
}
Run Code Online (Sandbox Code Playgroud)
错误
这会导致以下错误:
AutoMapper.dll中出现"AutoMapper.AutoMapperMappingException"类型的异常,但未在用户代码中处理
调试数据
首先,我通过调用确保没有配置错误:
Mapper.AssertConfigurationIsValid();
Run Code Online (Sandbox Code Playgroud)
我已经在我的代码中设置断点并尝试调试它,但我无法理解它.'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推回,所以它们不会对代码产生任何影响.
我的设置
实体和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";
}
}
Run Code Online (Sandbox Code Playgroud)
这是映射的目标类:
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; }
}
Run Code Online (Sandbox Code Playgroud)
映射 Authors 属性似乎存在问题。如果 Authors 序列为 null 或为空,则此聚合调用将引发异常。
.ForMember(x => x.Authors,
opt => opt.MapFrom(src => src.Book.Authors.Select(x => x.Name).Aggregate((i, j) => i + ", " + j)))
Run Code Online (Sandbox Code Playgroud)