Automapper缺少类型映射配置或不支持的映射 [英] Automapper missing type map configuration or unsupported mapping

查看:462
本文介绍了Automapper缺少类型映射配置或不支持的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个课程:

Class 1:(Domain)

Class 1 : (Domain)

public class Book
{
    public ObjectId Id { get; set; }
    public String ISBN { get; set; }
    public String Title { get; set; }
    public String Publisher { get; set; }
    public int? PageCount { get; set; }
    public Author Author { get; set; }
}

类2:(Repository)

Class 2 : (Repository)

public class Book
{        
    public ObjectId Id { get; set; }
    public String ISBN { get; set; }
    public String Title { get; set; }
    public String Publisher { get; set; }

    [BsonIgnoreIfNull]
    public int? PageCount { get; set; }
    public Author Author { get; set; }
}

为简单起见,我创建了2类具有相同的属性。我试图用代码映射2类:

For simple, i created 2 class have the same property. I tried to map 2 class with the code :

public static void SetAutoMapperConfiguration()
{
     Mapper.CreateMap<ME.Book.Book, DE.Book.Book>()
           .ForMember(dest => dest.PageCount, src => src.MapFrom(dest => dest.PageCount == null ? 0 : dest.PageCount))
           .ForMember(dest => dest.Author, src => src.MapFrom(dest => dest.Author == null ? null : dest.Author));
}

插入方法:

public async Task InsertBook(DE.Book book)
    {
        try
        {
            var bookCollections = GetDatabase().GetCollection<Book>(MongoCollection);
            Book savedBook = new Book(book.ISBN, book.Title, book.Publisher,
                new Author { FirstName = book.Author.FirstName, LastName = book.Author.LastName });

            Mapper.Map(savedBook, book); //Map failed
            await bookCollections.InsertOneAsync(savedBook);
        }
        catch(Exception e)
        {
            Console.WriteLine(e.GetBaseException());
        } 
    }

然后我得到一个错误:Automapper缺少类型映射配置或不支持的映射。

Then i got an error : Automapper missing type map configuration or unsupported mapping.

如果我删除了作者属性,它工作。

In case i remove Author property , it worked.

有人可以帮助我,我失踪了。感谢您的阅读我的问题和我的可怜的英语。

Can someone help me what i missing. Thanks for your reading my question and my poor English.

推荐答案

您最可能缺少作者的配置类。我假设你也有 ME.Book.Author DE.Book.Author ,所以你必须提供配置

You are most probably missing configuration for Author classes. I assume, you also have ME.Book.Author and DE.Book.Author, so you have to provide configure mapping also between these two classes.

扩展这样的配置:

public static void SetAutoMapperConfiguration()
{
    // fix namespaces and optionally provide mapping between properties
    Mapper.CreateMap<ME.Book.Author, DE.Book.Author>();    

    Mapper.CreateMap<ME.Book.Book, DE.Book.Book>()
           .ForMember(dest => dest.PageCount, src => src.MapFrom(dest => dest.PageCount == null ? 0 : dest.PageCount))
           .ForMember(dest => dest.Author, src => src.MapFrom(dest => dest.Author == null ? null : dest.Author));
}

这篇关于Automapper缺少类型映射配置或不支持的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆