2016-04-15 78 views
0

我想使用SQLite.Net和SQLiteNetExtensions建立一個SQLite數據庫。我似乎無法獲得在數據庫中創建的外鍵。我刪除了所有的表格,並使用模型類來重新創建它們。我檢查了SQLite管理器中的編譯指示,並打開了外鍵。在我的例子中,情節可以有很多樹。對於我可能錯過的任何建議?外鍵不會在SQLite數據庫使用SQLite.net創建

public class Tree 
    { 
     [PrimaryKey, AutoIncrement] 
     public int TreeId { get; set; } 
     [NotNull] 
     public int TreeNumber { get; set; } 
     [NotNull] 
     public double Dbhob { get; set; } 

     public double? Height { get; set; } 
     [NotNull,Default(value: false)] 
     public bool? HeightTree { get; set; } 
     public string QualityCode { get; set; } 
     [ForeignKey(typeof(Plot))] 
     public int PlotId { get; set; } 
     [ManyToOne] 
     public Plot PlotInverse { get; set; } 


    } 



    public class Plot 
    { 
     [PrimaryKey, AutoIncrement] 
     public int PlotId { get; set; } 
     [NotNull, Unique] 
     public System.Guid PlotGuid { get; set; } 
     [NotNull, MaxLength(60)] 
     public string Strata { get; set; } 
     [NotNull, MaxLength(60)] 
     public string PlotNumber { get; set; } 

     public DateTime MeasureDate { get; set; } 
     [MaxLength(70)] 
     public String AssessorLead { get; set; } 
     [MaxLength(60)] 
     public String AssessorCrew { get; set; } 
     [Default(value: 0)] 
     public double PlotArea { get; set; } 
     public double BasalArea { get; set; } 

     [OneToMany("PlotId","PlotId")] 
     public List<Tree> Trees { get; set; } 
    } 

回答

0

外鍵屬性沒有在SQLite數據庫SQLite的爲網創建沒有建立在它之上的外鍵和SQLite-Net的擴展支持。它們在運行時用於保存和加載相關對象。


作爲一個側面說明,你應該改變這一行:

[OneToMany("PlotId","PlotId")] 
public List<Tree> Trees { get; set; } 

要這樣:

[OneToMany("PlotId","PlotInverse")] 
public List<Tree> Trees { get; set; } 

或刪除它一起,並讓自動發現做它的神奇:

[OneToMany] 
public List<Tree> Trees { get; set; }