使用.net核心中的AutoMapper进行多对多关系映射

时间:2018-03-10 19:36:22

标签: c# automapper

我遇到多对多关系映射问题,我有以下数据:

public partial class Author
{
    public Author()
    {
        BookAuthors = new HashSet<BookAuthor>();
    }

    public int AuthorId { get; set; }
    public string AuthorName { get; set; }
    public string Nationality { get; set; }
    public string Biography { get; set; }
    public string Image { get; set; }

    public ICollection<BookAuthor> BookAuthors { get; set; }
}

public partial class Book
{
    public Book()
    {
  BookAuthors = new HashSet<BookAuthor>();
    }

    public int BookId { get; set; }
    public string Title { get; set; }
    public string Isbn { get; set; }
    public string ReleaseDate { get; set; }
    public decimal Price { get; set; }
    public int NoOfPages { get; set; }
    public decimal StarRating { get; set; }
    public string Description { get; set; }
    public string ImageUrl { get; set; }

    public ICollection<BookAuthor> BookAuthors { get; set; }
}

public partial class BookAuthor
{
    public int BookId { get; set; }
    public int AuthorId { get; set; }

    public Author Author { get; set; }
    public Book Book { get; set; }
}

public class AuthorDto
{

public int AuthorId { get; set; }
public string AuthorName { get; set; }
public string Nationality { get; set; }
public string Biography { get; set; }
public string Image { get; set; }

public IList<BookDto> Books { get; set; }
}

public class BookDto
{
public int BookId { get; set; }
public bool Title { get; set; }
public string Isbn { get; set; }
public string ReleaseDate { get; set; }
public decimal Price { get; set; }
public int NoOfPages { get; set; }
public decimal StarRating { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }

public IList<AuthorDto> Authors { get; set; }
}

映射代码:

CreateMap<Author, AuthorDto>().ForMember(dto => dto.Books, opt =>  
pt.MapFrom(x => x.BookAuthors.Select(y => y.Book).ToList()));

CreateMap<Book, BookDto>().ForMember(dto => dto.Authors, opt => 
opt.MapFrom(x => x.BookAuthors.Select(y => y.Author).ToList()));

CreateMap<BookAuthor, AuthorDto>();
CreateMap<BookAuthor, BookDto>();

如果我试图获得我收到的书籍: &#34;输入地图配置: 书 - &gt; BookDto BooksSinglePageApplication.Models.Book - &gt; BooksSinglePageApplication.Dtos.BookDto

属性: 标题---&gt; System.FormatException:String未被识别为有效的布尔值。    在System.Boolean.Parse(String value)....&#34;

如果我试图获得我收到的作者: &#34;映射类型: 列表1 -> IList 1 System.Collections.Generic.List 1[[BooksSinglePageApplication.Models.Author, BooksSinglePageApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IList 1 [[BooksSinglePageApplication.Dtos.BookDto,BooksSinglePageApplication,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] ---&gt; AutoMapper.AutoMapperConfigurationException: 找到了未映射的成员。查看下面的类型和成员。 作者 - &gt; BookDto(目的地成员列表) BooksSinglePageApplication.Models.Author - &gt; BooksSinglePageApplication.Dtos.BookDto(目标成员列表)&#34;

我知道有类似的问题已经问过,我检查了一下并尝试了一切,我真的不知道还有什么可以尝试。 非常感谢!

0 个答案:

没有答案