c# – AutoMapper.dll中出现类型“AutoMapper.AutoMapperMappingException”的异常,但未在用户代码中处理

前端之家收集整理的这篇文章主要介绍了c# – AutoMapper.dll中出现类型“AutoMapper.AutoMapperMappingException”的异常,但未在用户代码中处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
不知怎的,我的代码不再工作了(以前使用完全相同的代码).这就是问题:

代码

我正在尝试使用以下代码将一些对象映射到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));

用法

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

然后有一个stacktrace属性说:

at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()

at 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)
>实体框架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序列为空或为空,则此聚合调用将抛出异常.
.ForMember(x => x.Authors," + j)))
原文链接:https://www.f2er.com/csharp/95082.html

猜你在找的C#相关文章