EF Core唯一约束更新失败

时间:2019-04-13 14:58:49

标签: c# entity-framework uwp

我的对象结构非常复杂,但是已经设法添加和读取对象。现在我想更新一个实体,但是我得到了:

  

SqliteException:SQLite错误19:“唯一约束失败:Allergies.AllergyId”。

有什么想法吗?

患者分类:

public class Patient : IEquatable<Patient>
{
    public int PatientId { get; set; }
    public string Name { get; set; }
    [NotMapped]
    public ObservableCollection<Allergy> Allergies { get; set; }
    [NotMapped]
    public ObservableCollection<Disease> Diseases { get; set; }

    [NotMapped]
    private List<PatientDisease> _patientDiseases;

    public List<PatientDisease> PatientDiseases {
        get { return _patientDiseases = _patientDiseases ?? new List<PatientDisease>(); }
        set { _patientDiseases = value; this.Diseases = new ObservableCollection<Disease>(value.Select(x => x.Disease).ToList()); }
    }

    [NotMapped]
    private List<PatientAllergy> _patientAllergies;

    public List<PatientAllergy> PatientAllergies {
        get { return _patientAllergies = _patientAllergies ?? new List<PatientAllergy>(); }
        set { _patientAllergies = value; this.Allergies = new ObservableCollection<Allergy>(value.Select( x => x.Allergy).ToList()); }
    }

    public bool Equals(Patient other)
    {
        if (other is null)
            return false;

        return this.PatientId == other.PatientId;
    }

    public override bool Equals(object obj) => Equals(obj as Patient);
    public override int GetHashCode() => (PatientId).GetHashCode();
}

过敏类别:

public class Allergy : IEquatable<Allergy>
{
    public int AllergyId { get; set; }
    public string Name { get; set; }

    public List<MedicationAllergy> MedicationAllergies { get; set;}
    public List<PatientAllergy> PatientAllergies { get; set; }

    public bool Equals(Allergy other)
    {
        if (other is null)
            return false;

        return this.AllergyId == other.AllergyId;
    }

    public override bool Equals(object obj) => Equals(obj as Allergy);
    public override int GetHashCode() => (AllergyId).GetHashCode();
}

发生更新的收集器方法:

public void EditPatient(Patient SelectedPatient)
    {
        using (var db = new DatabaseContext())
        {
            var entity = db.Patients.Include(x => x.PatientAllergies)
                    .ThenInclude(x => x.Allergy)
                    .Include(x => x.PatientDiseases)
                    .ThenInclude(x => x.Disease)
                    .ThenInclude(x => x.DiseaseMedications)
                    .ThenInclude(x => x.Medication)
                    .ThenInclude(x => x.MedicationAllergies)
                    .ThenInclude(x => x.Allergy)
                    .FirstOrDefault(item => item.PatientId == SelectedPatient.PatientId);

            if (entity != null)
            {
                entity.Name = SelectedPatient.Name;
                entity.PatientAllergies = SelectedPatient.PatientAllergies;
                entity.PatientDiseases = SelectedPatient.PatientDiseases;
                entity.Allergies = SelectedPatient.Allergies;
                entity.Diseases = SelectedPatient.Diseases;

                db.Patients.Update(entity);
                db.SaveChanges();    
            }             
        }
    }

0 个答案:

没有答案