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;
}
}