2017-07-03 137 views
0

我有關係一對一的缺少類型映射配置或不支持的映射,一對一的關係

public class Book 
    { 
     public int BookId { get; set; } 
     public string Name { get; set; } 
     public string Annotation { get; set; } 
     public virtual File File { get; set; } 
     public int? SeriesId { get; set; } 
     public DateTime UploadDate { get; set; } 
     public virtual ICollection<Comment> Comments { get; set; } 
     public virtual ICollection<Author> Authors { get; set; } 
     public virtual ICollection<Genre> Genres { get; set; } 
     public virtual ICollection<Mark> Marks { get; set; } 
     public Book() 
     { 
      Comments = new List<Comment>(); 
      Authors = new List<Author>(); 
      Genres = new List<Genre>(); 
     } 
    } 

public class File 
    { 
     [Key,ForeignKey("Book")] 
     public int BookId { get; set; } 
     public string FileName { get; set; } 
     public string ContentType { get; set; } 
     public byte[] Content { get; set; } 
     public virtual Book Book { get; set; } 
    } 

我希望將數據傳輸到類:

public class BookDO 
{ 
    public int BookId { get; set; } 
    public string Name { get; set; } 
    public string Annotation { get; set; } 
    public virtual FileDO File { get; set; } 

} 


public class FileDO 
    { 

     public int BookId { get; set; } 
     public string FileName { get; set; } 
     public string ContentType { get; set; } 
     public byte[] Content { get; set; } 
     public virtual BookDO Book { get; set; } 
    } 

在這樣的方式:

var books = Database.Books.GetAll().ToList(); 
      Mapper.Initialize(cf => cf.CreateMap<Book, BookDO>()); 
      return Mapper.Map<List<Book>, List<BookDO>>(books); 

但我越來越缺少類型映射配置或不支持的映射。

映射類型: 文件 - > FileDO Domain.File - > BusinessLogic.Data_Objects.FileDO 也許我需要初始化一個更映射到文件映射到FileDO或修改現有映射配置?請幫幫我。

回答

0

是的,你還需要創建一個地圖File - >FileDo。此映射必須針對Book - >BookDo所用的相同映射器進行配置。

這是很好的做法來包裝你的映射配置爲AutoMapper.Profile

using AutoMapper; 
public class BookMappingProfile: Profile { 
    public BookMappingProfile() { 
     CreateMap<Book, BookDo>(); 
     CreateMap<File, FileDo>(); 
    } 
} 

然後用這些配置文件初始化映射:

Mapper.Initialize(cfg => { 
    cfg.AddProfile<BookMappingProfile>(); 
    cfg.AddProfile<MyOtherProfile>(); 
}); 
相關問題