我有以下json對象,我試圖將它們存儲在sqlite中的兩個不同表中。這裏是場景,我首先下載一組學生並檢查數據庫,並且一切看起來都很棒。OneToMany關係數據庫
但是,當我得到另一組學生插入到現有的表。對於第一組數據集,很少有StudentId
的值爲NULL,但最新的數據集的StudentId
是正確的。
更詳細地說,您在StudentSpecifications
表中的許多人具有相同的studentId
。如果要插入的第二個數據集具有相同的StudentId
,那麼這些數據集將影響第一個數據集並使其成爲NULL
,但第二個數據集StudentId
是正確的。
StudentId用作ForeignKey
。我懷疑我正在使用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
[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;
}
插入到表
public bool AddOrUpdate(IEnumerable<T> entities, bool withChildren = false)
{
var db = _factory.GetConnectionWithLock();
using (db.Lock())
{
db.InsertOrReplaceAllWithChildren(entities, true);
return true;
}
}
你可以發佈'Student.cs'的代碼嗎?看起來不完整,你不能用OneToMany類來註釋。 – redent84
@ redent84,你是對的,這是我的錯誤。我已添加缺失的一塊。 – hotspring