EF核心中的automapper和多对多关系

时间:2018-04-22 15:46:05

标签: asp.net-core ef-code-first mapping entity-framework-core automapper

我有两个具有多对多关系和联接表的模型:

 public class BookView
    {
        public int BookId { get; set; }
        public string Name { get; set; }
        public string AuthorName { get; set; }
        public int YearOfPublishing { get; set; }
        public LibraryType Type { get; set; }

        public IEnumerable<PublicHouseView> PublicHouses { get; set; }

    }

然后我需要将View模型映射到模型。 View模型如下所示:

CreateMap<Book, BookView>()
                .ForMember(bv => bv.PublicHouses, opt => opt.MapFrom(x => x.PublicHouses.Select(y => y.PublicHouse).ToList()));

创建地图:

public IEnumerable<BookView> Get()
        {
            var books = _context.Books
                .Include(b => b.PublicHouses).ThenInclude(bph => bph.PublicHouse).ToList();
            return mapper.Map<List<Book>, List<BookView>>(books);
        }

但我有一个例外(错误映射类型,缺少类型映射配置或不支持的映射。 )这里:

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var daysToCompare = days.map(m => m.toLowerCase());
do {
    var response = (prompt("Choose a day of the week: ") || '').toLowerCase();
	switch(true) {
		case ((response = daysToCompare.indexOf(response)) >= 0):
			alert("You have chosen day: " + days[response]);
			response = true;
			break;
		default:
			alert("That is not a choice!");
			response = false;
	}
}
while(!response);

0 个答案:

没有答案