Automapper и вложенные списки - C#
Формулировка задачи:
Не получается конвертировать объекты. Работает ли Automapper со вложенными списками?
Вылетает исключение: An unhandled exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll
Additional information: Error mapping types.
Заранее спасибо
Буду благодарен, если напишите, что ещё нужно уточнить, чтобы проблема стала яснее
using AutoMapper;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace automappertest
{
public class PairDTO
{
public int Num { get; set; }
public string State { get; set; }
public string Name { get; set; }
public string Teacher { get; set; }
public string Cabinet { get; set; }
}
public class DayDTO
{
public List<PairDTO> Pair { get; set; }
public string Name { get; set; }
}
public class SheduleGroupDTO
{
public List<DayDTO> Day { get; set; }
public string Name { get; set; }
}
public class SheduleDTO
{
public long SheduleId { get; set; }
public DateTime Date { get; set; }
public bool Type { get; set; }
public List<SheduleGroupDTO> SheduleGroups { get; set; }
}
class Program
{
static void Main(string[] args)
{
SheduleDTO sheduleDTO = new SheduleDTO()
{
Date = DateTime.Now,
Type = true,
SheduleGroups = new List<SheduleGroupDTO>()
{
new SheduleGroupDTO()
{
Name = "ИП-403",
Day = new List<DayDTO>()
{
new DayDTO()
{
Name = "Понедельник",
Pair = new List<PairDTO>()
{
new PairDTO()
{
Num = 1,
State = "stand",
Name = "Название предмета",
Teacher = "Преподаватель",
Cabinet = "111"
}
}
}
}
}
}
};
Mapper.Initialize(cfg => cfg.CreateMap<SheduleDTO, Shedule>()
);
var shedule = Mapper.Map<SheduleDTO, Shedule>(sheduleDTO);
}
}
public partial class Shedule
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long SheduleId { get; set; }
public DateTime? Date { get; set; }
public bool? Type { get; set; }
public virtual List<SheduleGroup> SheduleGroups { get; set; }
public Shedule()
{
SheduleGroups = new List<SheduleGroup>();
}
}
public partial class SheduleGroup
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long SheduleGroupId { get; set; }
public string Name { get; set; }
public long? SheduleId { get; set; }
public virtual Shedule Shedules { get; set; }
public virtual ICollection<Day> Days { get; set; }
public SheduleGroup()
{
Days = new HashSet<Day>();
}
}
public partial class Day
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long DayId { get; set; }
public string Name { get; set; }
public long SheduleGroupId { get; set; }
public virtual SheduleGroup SheduleGroups { get; set; }
public virtual ICollection<Pair> Pairs { get; set; }
public Day()
{
Pairs = new List<Pair>();
}
}
public partial class Pair
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long PairId { get; set; }
public int Num { get; set; }
public string State { get; set; }
public string Lesson { get; set; }
public string Teacher { get; set; }
public string Cabinet { get; set; }
public long? DayId { get; set; }
public virtual Day Days { get; set; }
}
}Решение задачи: «Automapper и вложенные списки»
textual
Листинг программы
namespace automappertest
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using AutoMapper;
public class PairDTO
{
public int Num { get; set; }
public string State { get; set; }
public string Name { get; set; }
public string Teacher { get; set; }
public string Cabinet { get; set; }
}
public class DayDTO
{
public List<PairDTO> Pair { get; set; }
public string Name { get; set; }
}
public class SheduleGroupDTO
{
public List<DayDTO> Day { get; set; }
public string Name { get; set; }
}
public class SheduleDTO
{
public long SheduleId { get; set; }
public DateTime Date { get; set; }
public bool Type { get; set; }
public List<SheduleGroupDTO> SheduleGroups { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
var sheduleDTO = new SheduleDTO
{
Date = DateTime.Now,
Type = true,
SheduleGroups = new List<SheduleGroupDTO>
{
new SheduleGroupDTO
{
Name = "ИП-403",
Day = new List<DayDTO>
{
new DayDTO
{
Name = "Понедельник",
Pair = new List<PairDTO>
{
new PairDTO
{
Num = 1,
State = "stand",
Name = "Название предмета",
Teacher = "Преподаватель",
Cabinet = "111"
}
}
}
}
}
}
};
Mapper.Initialize(cfg =>
{
cfg.CreateMap<SheduleDTO, Shedule>();
cfg.CreateMap<SheduleGroupDTO, SheduleGroup>();
cfg.CreateMap<DayDTO, Day>();
cfg.CreateMap<PairDTO, Pair>();
});
var shedule = Mapper.Map<SheduleDTO, Shedule>(sheduleDTO);
}
}
public class Shedule
{
public Shedule()
{
SheduleGroups = new List<SheduleGroup>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long SheduleId { get; set; }
public DateTime? Date { get; set; }
public bool? Type { get; set; }
public virtual List<SheduleGroup> SheduleGroups { get; set; }
}
public class SheduleGroup
{
public SheduleGroup()
{
Days = new HashSet<Day>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long SheduleGroupId { get; set; }
public string Name { get; set; }
public long? SheduleId { get; set; }
public virtual Shedule Shedules { get; set; }
public virtual ICollection<Day> Days { get; set; }
}
public class Day
{
public Day()
{
Pairs = new List<Pair>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long DayId { get; set; }
public string Name { get; set; }
public long SheduleGroupId { get; set; }
public virtual SheduleGroup SheduleGroups { get; set; }
public virtual ICollection<Pair> Pairs { get; set; }
}
public class Pair
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public long PairId { get; set; }
public int Num { get; set; }
public string State { get; set; }
public string Lesson { get; set; }
public string Teacher { get; set; }
public string Cabinet { get; set; }
public long? DayId { get; set; }
public virtual Day Days { get; set; }
}
}