2

我有三个实体。

public class Book
{
    public string Name {get;set;}
}
public class Author
{
    public string AuthorName {get;set;}
}

public class BookDTO
{
    public string Name {get;set;}
    public string AuthorName {get;set;}
}

如何映射BookAuthorBookDTO?以及如何映射BookDTOBookAuthor?我在我的解决方案中使用了自动映射器。

4

1 回答 1

5

您可以为每个创建一个映射:

Mapper.CreateMap<Book, BookDTO>();
Mapper.CreateMap<Author, BookDTO>();

然后像这样使用它:

Book b = new Book { Name = "Gulliver's Travels" };
Author a = new Author { AuthorName = "Jonathan Swift" };

var dto = Mapper.Map<Book, BookDTO>(b);

Mapper.Map<Author, BookDTO>(a, dto);
于 2012-08-17T00:53:00.883 回答