Asp.net mvc 缺少类型映射配置或不支持的映射,一对一关系

Asp.net mvc 缺少类型映射配置或不支持的映射,一对一关系,asp.net-mvc,automapper,Asp.net Mvc,Automapper,我有一对一的关系 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 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; }
    }
以这种方式:

 var books = Database.Books.GetAll().ToList();
            Mapper.Initialize(cf => cf.CreateMap<Book, BookDO>());
            return Mapper.Map<List<Book>, List<BookDO>>(books);
var books=Database.books.GetAll().ToList();
初始化(cf=>cf.CreateMap());
返回Mapper.Map(书籍);
但是我得到了丢失的类型映射配置或不支持的映射

映射类型: 文件->文件执行 Domain.File->BusinessLogic.Data\u Objects.FileDO
也许我需要再初始化一个映射器来将文件映射到FileDO,或者修改现有的映射器配置?请帮帮我。

是的,您还需要为
文件创建映射。必须为
Book
->
BookDo
使用的相同映射器配置此映射

将映射配置包装到
AutoMapper.Profile
中是一种很好的做法:

using AutoMapper;
public class BookMappingProfile: Profile {
    public BookMappingProfile() {
        CreateMap<Book, BookDo>();
        CreateMap<File, FileDo>();
    }
}
使用AutoMapper;
公共类BookMappingProfile:Profile{
公共BookMappingProfile(){
CreateMap();
CreateMap();
}
}
然后使用以下配置文件初始化映射器:

Mapper.Initialize(cfg => { 
    cfg.AddProfile<BookMappingProfile>(); 
    cfg.AddProfile<MyOtherProfile>();
});
Mapper.Initialize(cfg=>{
AddProfile();
AddProfile();
});
Mapper.Initialize(cfg => { 
    cfg.AddProfile<BookMappingProfile>(); 
    cfg.AddProfile<MyOtherProfile>();
});