创建 MappingConfiguration 时具有现有键的项目
item with existing key, when creating MappingConfiguration
public class ClinicController
{
private List<Employee> Employees;
private List<Patient> Patients;
private Mapper _objectMapper;
public ClinicController()
{
Employees = new List<Employee>();
Patients = new List<Patient>();
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Doctor, DoctorDto>().ReverseMap();
//cfg.CreateMap<Employee, Doctor>();
//cfg.CreateMap<DoctorDto, Doctor>();
//cfg.CreateMap<Employee, EmployeeDto>();
//cfg.CreateMap<Patient, DoctorDto>();
});
_objectMapper = new Mapper(config);
}
}
当我实例化此 class 时,无论何时创建配置,都会引发异常。它说:
"An item with the same key has already been added"
我一直在阅读相同问题的问题,但我似乎无法弄清楚这个问题。调试代码发现程序永远无法初始化映射器,它在初始化配置之前就崩溃了。
我忽略了什么?
类:
public abstract class PersonDto
{
public string Name { get; set; }
public string Surnames { get; set; }
public string Data{ get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Direction { get; set; }
}
public abstract class Person
{
public string Name { get; set; }
public string Surnames { get; set; }
public string Data { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Direction { get; set; }
}
public class Patient : Person
{
public string Service { get; set; }
public float Pay { get; set; }
}
public class Employee : Person
{
public string Job { get; set; }
public float Salary { get; set; }
}
public class Doctor : Employee
{
public new string Job = "Doctor";
}
这些 class 具有与原件相同的属性(我没有包括这些方法,因为它们无关紧要)。 Dto 遵循与实体本身相同的结构。
如果 Doctor DTO 也有 "overwritten" 作业 属性,它将崩溃,因为会有 2 个作业 "Keys"。不要使用新字符串并找到另一种方法来做到这一点。
因为你正在将医生映射到他们的 DTO,所以不需要指定作业的值(假设它没有被修改和存储在数据库中)。
public class ClinicController
{
private List<Employee> Employees;
private List<Patient> Patients;
private Mapper _objectMapper;
public ClinicController()
{
Employees = new List<Employee>();
Patients = new List<Patient>();
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Doctor, DoctorDto>().ReverseMap();
//cfg.CreateMap<Employee, Doctor>();
//cfg.CreateMap<DoctorDto, Doctor>();
//cfg.CreateMap<Employee, EmployeeDto>();
//cfg.CreateMap<Patient, DoctorDto>();
});
_objectMapper = new Mapper(config);
}
}
当我实例化此 class 时,无论何时创建配置,都会引发异常。它说:
"An item with the same key has already been added"
我一直在阅读相同问题的问题,但我似乎无法弄清楚这个问题。调试代码发现程序永远无法初始化映射器,它在初始化配置之前就崩溃了。
我忽略了什么?
类:
public abstract class PersonDto
{
public string Name { get; set; }
public string Surnames { get; set; }
public string Data{ get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Direction { get; set; }
}
public abstract class Person
{
public string Name { get; set; }
public string Surnames { get; set; }
public string Data { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Direction { get; set; }
}
public class Patient : Person
{
public string Service { get; set; }
public float Pay { get; set; }
}
public class Employee : Person
{
public string Job { get; set; }
public float Salary { get; set; }
}
public class Doctor : Employee
{
public new string Job = "Doctor";
}
这些 class 具有与原件相同的属性(我没有包括这些方法,因为它们无关紧要)。 Dto 遵循与实体本身相同的结构。
如果 Doctor DTO 也有 "overwritten" 作业 属性,它将崩溃,因为会有 2 个作业 "Keys"。不要使用新字符串并找到另一种方法来做到这一点。 因为你正在将医生映射到他们的 DTO,所以不需要指定作业的值(假设它没有被修改和存储在数据库中)。