2012-08-17 116 views
1

我有三個實體。如何使用automapper將兩個實體映射到一個實體?

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? 以及如何將BookDTO映射到BookAuthor? 我在我的解決方案中使用了自動映射器。

回答

4

你可以爲每個創建一個映射:

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); 
+0

哦!它是如此容易!兩次地圖! – Dozer 2012-08-17 03:42:51

相關問題