简体   繁体   English

一对多关系数据库

[英]OneToMany Relational Database

I have the following json object, where I am trying to store them in two different tables in the sqlite. 我有以下json对象,我试图将它们存储在sqlite的两个不同表中。 Here is the scenario, I download a set a students first and check the database, and everything looks great. 在这种情况下,我首先下载一组学生并检查数据库,一切看起来都很不错。

However, when I get another set of students to insert to the existing tables. 但是,当我让另一组学生插入到现有表中时。 few StudentId 's gets NULL for the first set of the dataset, but the latest dataset's StudentId is right. 很少有StudentId的第一组数据集为NULL,但最新数据集的StudentId是正确的。

In more detail, you many have same studentId in the StudentSpecifications table. 详细地说,你多有相同studentIdStudentSpecifications表。 If the second dataset which is going to be inserted has the same StudentId , those affects the first dataset and make them NULL , but the second dataset StudentId is right. 如果要插入的第二个数据集具有相同的StudentId ,则会影响第一个数据集并使它们为NULL ,但第二个数据集StudentId是正确的。

StudentId is used as a ForeignKey . StudentId用作ForeignKey I suspect that I am using OnetoMany relationship, but I do not know how to handle? 我怀疑我正在使用OnetoMany关系,但是我不知道该如何处理?

Student: [
{
  StudentId: "3322449c-cd89-4633-ae3a-4578572eeeee",
  Name: "Joseph Peter",
  Qualification: "1222449c-efds-rfcs-asde-8546242kkkkk",
  StudentSpecifications: [
  {
    Id: "45e63840-0dc3-4296-a83d-97cc462b2dac",
    EnrollDate: "2016-08-05T09:40:21.233",
    GraduationDate: "2017-06-05T09:40:21.233",
   },
   {
    Id: "25fffe40-0dc3-4296-a83d-97cc462b2dac",
    EnrollDate: "2015-07-05T09:40:21.233",
    GraduationDate: "2016-08-05T09:40:21.233",
   },
  }
]

Student.cs 学生.cs

[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<StudentSpecification> StudentSpecifications { get; set; }

public Student(StudentDto dto)
{
    Id = dto.StudentId.ToString();
    Name = dto.Name;
    QualificationId = dto.QualificationId.ToString();
    StudentSpecifications= dto.StudentSpecification.Select(x => new StudentSpecification(x, Id)).ToList();
} 

StudentSpecification.cs StudentSpecification.cs

[Table("STUDENT_SPECIFICATIONS")]
public class StudentSpecification
{
    [PrimaryKey]
    public string Id{get;set;}
    [ForeignKey(typeof(Student))]
    public string StudentId { get; set; }
    public DateTime EnrollDate{ get; set; }
    public DateTime GraduationDate{ get; set; }

    public StudentSpecification(StudentDto dto, string studentId)
    {
        Id = Guid.NewGuid().ToString();
        StudentId = studentId;
        EnrollDate= dto.EnrollDate;
        GraduationDate= dto.GraduationDate;
    }

Inserting into the table 插入桌子

public bool AddOrUpdate(IEnumerable<T> entities, bool withChildren = false)
{
  var db = _factory.GetConnectionWithLock();
  using (db.Lock())
  {
     db.InsertOrReplaceAllWithChildren(entities, true);
     return true;
   }
}

You're overriding the relationship each time you save your new dataset here: 每次在此处保存新数据集时,您都将覆盖关系:

StudentSpecifications= dto.StudentSpecification.Select(x => new StudentSpecification(x, Id)).ToList();

So your old StudentSpecifications will be removed from the relationship in the database by setting the foreign key to null . 因此,通过将外键设置为null将从数据库中的关系中删除旧的StudentSpecifications

The issue is that you want to merge these results, so you can either load previous elements from database and merge them manually, or handle the relationship manually. 问题是您要合并这些结果,因此您可以从数据库加载以前的元素并手动合并它们,也可以手动处理关系。

As you are already assigning the foreign key manually, you can just insert the objects using plain SQLite-Net methods: 由于您已经在手动分配外键,因此只需使用简单的SQLite-Net方法插入对象即可:

db.InsertOrReplace(student);
for (var spec in student.StudentSpecifications) {
    db.InsertOrReplace(spec);
}

This way your old relationships won't get removed and the results will be merged next time you load them from database. 这样,您的旧关系将不会被删除,并且下次您从数据库中加载它们时,结果将被合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM