2017-07-03 78 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>(); 
}); 
相关问题