2015-02-10 73 views
0

我試圖在SQlite兩個表之間建立一個簡單的關係。 I'm下面是創建此類指南:關於SQlite關係的初學者

public class Car 
    { 
     [PrimaryKey, AutoIncrement] 
     public int Id { get; set; } 
     public string Brand { get; set; } 
     public int Milage { get; set; } 
     public int NumberOfWheels { get; set; } 
    } 

在引導他創造這在其CTOR

public CarRepository(SQLiteConnection connection) 
     { 
      _connection = connection; 
      _connection.CreateTable<Car>(); 
     } 

在我自己的例子創建此表的存儲庫,我想添加第二個表Driver

public class Driver 
     { 

      public int DriverId { get; set; } 
      public string Name{ get; set; } 

     } 

但是如何建立一個關係,如果一個司機可以有很多車?有了實體框架,我也只會說:

public virtual ICollection<Car> DriversCars { get; set; } 

Driver -class和:

public virtual int DriverId { get; set; } 

在汽車類。

是否有可能在SQlite中以類似的方式執行此操作,或者我可能必須以某種方式使用Sql語法?

+0

這是.NET代碼? – 2015-02-10 09:52:57

+0

是的,即時創建一個Windows應用程序。 – bugsy 2015-02-10 09:55:15

+0

這裏是使用教程,雖然我只對windows-part感興趣: http://www.johankarlsson.net/2014/04/setting-up-sqlite-on-ios-android-and.html – bugsy 2015-02-10 09:56:34

回答

1

如果你想使用實體框架,似乎有一種方法可以使用System.Data.SQLite nuget包來實現 - 參見另一個Blob文章,它似乎正是通過使用該軟件包來完成的:Entity Framework on SQLite

+0

感謝你,我會看看這個。 – bugsy 2015-02-10 10:12:52