2015-04-01 26 views
0

請原諒的代碼在牆上,但我需要設置舞臺....多對多關係在SQLite的擴展不更新

public class Student 
{ 
    public string Name { get; set; } 
    [PrimaryKey] 
    public string Id { get; set; } 

    [ManyToMany(typeof(StudentStaff))]   
    public List<Staff> Teachers { get; set; } 

    [ManyToMany(typeof(StudentGuardian))] 
    public List<Guardian> Guardians { get; set; } 
} 

    public class Guardian 
    { 
     [PrimaryKey] 
     public string Id { get; set; } 
     public string Name{get;set;}  

     [ManyToMany(typeof(StudentGuardian))] 
     public List<Student> Students { get; set; } 
    } 

public class Staff 
{ 
    [PrimaryKey] 
    public string Id { get; set; } 
    public string Name { get; set; } 

    [ManyToMany(typeof(StudentStaff))] 
    public List<Student> Students { get; set; } 
} 

public class StudentStaff 
{ 
    [ForeignKey(typeof(Student))] 
    public string StudentId { get; set; } 
    [ForeignKey(typeof(Staff))] 
    public string StaffId { get; set; } 
} 

public class StudentGuardian 
    { 
     [ForeignKey(typeof(Student))] 
     public string StudentId { get; set; } 
     [ForeignKey(typeof(Guardian))] 
     public string GuardianId { get; set; } 
} 

鑑於這些,我應該怎麼插入的學生,工作人員及監護人類進入數據庫,同時保持關係?我應該注意到服務器返回填充的學生記錄,所以這是我的出發點。

我試過conn.InsertOrReplaceWithChidlren(student); ...插入了學生,學生和學生監護人記錄,但不是實際的工作人員或監護人。 我試圖 conn.InsertOrReplaceWithChildren(student); conn.InsertOrReplaceWithChildren(student.Teachers); conn.InsertOrReplaceWithChildren(student.Guardians); 奇怪的是,結束了工作人員,監護人和老師插入,但沒有交集表中的任何數據...

--update --- 我只是想 conn.InsertOrReplaceWithChildren(student.Teachers); conn.InsertOrReplaceWithChildren(student.Guardians); conn.InsertOrReplaceWithChildren(student); 它的工作完美...問題是爲什麼?爲什麼多數人似乎依賴於操作秩序?

回答

1

SQLite-Net Extensions要求所有對象都被分配了一個唯一的ID來建立關係。使用AutoIncrement主鍵時,這意味着所有對象都已被插入到數據庫中。

如果尚未設置反相關係,則順序也可能會產生影響,因此設置反比關係或將這些屬性設置爲ReadOnly以避免副作用通常是個好主意。

這就是說,SQLite-Net Extensions提供了幫助完成此任務的實用方法。在這種情況下,您可以使用級聯操作來執行單個插入。爲了實現級聯操作,定義關係屬性的CascadeOperations屬性:

public class Student 
{ 
    public string Name { get; set; } 
    [PrimaryKey] 
    public string Id { get; set; } 

    [ManyToMany(typeof(StudentStaff), CascadeOperations = CascadeOperation.All)]   
    public List<Staff> Teachers { get; set; } 

    [ManyToMany(typeof(StudentGuardian), CascadeOperations = CascadeOperation.All))] 
    public List<Guardian> Guardians { get; set; } 
} 

    public class Guardian 
    { 
     [PrimaryKey] 
     public string Id { get; set; } 
     public string Name{get;set;}  

     [ManyToMany(typeof(StudentGuardian), CascadeOperations = CascadeOperation.All, ReadOnly = true))] 
     public List<Student> Students { get; set; } 
    } 

public class Staff 
{ 
    [PrimaryKey] 
    public string Id { get; set; } 
    public string Name { get; set; } 

    [ManyToMany(typeof(StudentStaff), CascadeOperations = CascadeOperation.All, ReadOnly = true))] 
    public List<Student> Students { get; set; } 
} 

在此示例中,我還包括ReadOnly屬性,以使GuardianStaff關係到Student只讀屬性,因此SQLite的-Net的擴展會忽略這個將對象保存到數據庫時的關係。

然後,你可以使用任何的SQLite-Net的擴展方法的recursive參數true

conn.InsertOrReplaceWithChildren(student, recursive: true); 

SQLite的-Net的擴展將處理反向關係,關係正常循環。

+0

嗯,我喜歡只讀,我會給其餘的一個旋轉,讓你知道。我不是100%確定你爲什麼提到自動增量鍵。我的樣品根本就沒有。 – user3589700 2015-04-08 00:09:16