这是我的班级
public class SmartPhone : BaseEntity
{
[MaxLength(50, ErrorMessage = "Maximum length 50 character"), MinLength(1, ErrorMessage = "Min length 1 character")]
public string Name { get; set; }
public decimal Price { get; set; }
public virtual Manufacture Manufacture { get; set; }
}
此类具有Manufacture
的外键。我用EF
public class Manufacture : BaseEntity
{
[Required]
[MaxLength(50, ErrorMessage = "The Manufacture Name must be a string and max length 50")]
public string ManufactureName { get; set; }
public virtual ICollection<SmartPhone> SmartPhones { get; set; }
}
当我使用mapper到我的DTo对象时
var mapper1 = Mapper.CreateMap<SmartPhoneDto, SmartPhone>();
mapper1.ForMember(d => d.Manufacture, s => s.MapFrom(a => a.ManufactureDto));
var mapper = Mapper.CreateMap<SmartPhone, SmartPhoneDto>();
mapper.ForMember(d => d.ManufactureDto, s => s.MapFrom(a => a.Manufacture));
当我调入SaveChanges
时,插入我的数据库时会崩溃:
违反PRIMARY KEY约束'PK_dbo.Manufactures'。无法在对象'dbo.Manufactures'中插入重复键。
请帮我解决一下