C# 插入ASPNET核心时出现重复信息错误

C# 插入ASPNET核心时出现重复信息错误,c#,mysql,asp.net-core,entity-framework-core,core,C#,Mysql,Asp.net Core,Entity Framework Core,Core,当我添加一名员工时,我会询问公司的数据,添加下一名员工会再次询问我公司的数据,并生成重复记录。如果这两名员工来自同一家公司,则应通过我的验证,以便我不会重新注册该公司 public class Company { [Key] public int Id { get; set; } [Required] [MaxLength(45)] public string Code { get; set; } [Required] public str

当我添加一名员工时,我会询问公司的数据,添加下一名员工会再次询问我公司的数据,并生成重复记录。如果这两名员工来自同一家公司,则应通过我的验证,以便我不会重新注册该公司

public class Company
{
    [Key]
    public int Id { get; set; }
    [Required]
    [MaxLength(45)]
    public string Code { get; set; }
    [Required]
    public string Name { get; set; }
    public string BussinesName { get; set; }

    public string WebAddress { get; set; }

    public virtual ICollection<Employee> Employees { get; set; }

}
public class Employee
{
    [Key]
    public int Id { get; set; }
    public int EmployeeNumber { get; set; }
    [Required]
    public Company Company { get; set; }
    [Required]
    public bool Active { get; set; }
}
对缺失公司数据的回应如下:

{
  "Person": [
    "The Person field is required."
 ],
  "Company.Code": [
  "The Code field is required."
 ],
   "Company.Name": [
   "The Name field is required."
 ]
 }
公司详情

{
"Person": {
  "lastNamePat": "Juan",
  "lastNameMat": null,
  "firstName": "Lopez"
},
 "Company" :{
    "Code": "XXX",
    "Name": "test"
}
}

公司表


如何验证不重复的信息?

您可以尝试以下方法。基本上,如果已经存在,您需要将“Company”设置为null

        if (ModelState.IsValid)
        {
            try
            {
                if (data != null && data.Company != null)
                {
                    if (_context.Employee.Company.Any(x => x.Code == data.Company.Code))
                    {
                        data.Company = null;
                        _context.Employee.Add(data);
                    }
                    else
                    {
                        _context.Employee.Add(data);
                    }
                }

                return Ok(_context.SaveChanges());
            }
            catch (Exception ex)
            {
            }
        }

        return BadRequest(ModelState);

您可以使用抽象验证器。 模型有效状态已配置为验证规则。 范例


您的
员工
公司
实体之间似乎没有外键。但是,当我将公司定义为空时,我不会为该员工引用该公司,我需要保存信息,该员工属于该公司,但不会再次添加该公司,这将双重分配现有公司,而不是null
        if (ModelState.IsValid)
        {
            try
            {
                if (data != null && data.Company != null)
                {
                    if (_context.Employee.Company.Any(x => x.Code == data.Company.Code))
                    {
                        data.Company = null;
                        _context.Employee.Add(data);
                    }
                    else
                    {
                        _context.Employee.Add(data);
                    }
                }

                return Ok(_context.SaveChanges());
            }
            catch (Exception ex)
            {
            }
        }

        return BadRequest(ModelState);
RuleFor(c => c.ModuleId)
   .NotEmpty()
   .WithMessage("Module id is required.")
   .Must(BeAnExistingModule)
   .WithMessage("Module does not exist.");

private bool BeAnExistingModule(AddVersion cmd, Guid moduleId)
{
    return _moduleRules.DoesModuleExist(cmd.SiteId, moduleId);
}