0

I have the following json object, where I am trying to store them in two different tables in the 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.

In more detail, you many have same studentId in the StudentSpecifications table. 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 is used as a ForeignKey. I suspect that I am using OnetoMany relationship, but I do not know how to handle?

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

[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

[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;
   }
}
casillas
  • 16,351
  • 19
  • 115
  • 215

1 Answers1

1

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.

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:

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.

redent84
  • 18,901
  • 4
  • 62
  • 85