2009-12-01 36 views
0

我修改了System.Data.SQLite以使用SQLite引擎的最新版本,該引擎自動執行外鍵而不使用自定義觸發器。SQLite PRAGMA foreign_keys問題

我也在使用SubSonic 2.x,但是這適用於使用SQLite的任何ORM框架,它們都是'提前遲交'。

如何確保在每個SQLiteConnection.Open()上調用聲明'PRAGMA foreign_keys = true'?它必須被調用或者外鍵不起作用。

回答

2

爲了解決這個問題,我在SQLiteConnection類的ConnectionString中添加了一個'Foreign Keys'屬性。

外鍵= ON 外鍵= OFF

1

,如果你想使用最新版本的SQLite,只需使用System.Data的ManagedOnly版本你並不需要修改System.Data.SQLite。 SQLite,然後只用最新版本替換sqlite3.dll。爲了啓用外鍵支持,我只需執行一個啓用外鍵支持的sql語句。例如

 string databasePath = "Your database path here"; 
     string connectionString = "Data Source=" + databasePath; 
     connection = new SQLiteConnection(connectionString); 
     connection.Open(); 

     const string sqlString = "PRAGMA foreign_keys = ON;"; 
     SQLiteCommand command = new SQLiteCommand(sqlString, connection); 
     command.ExecuteNonQuery(); 
+0

這是一個好主意。我使用亞音速,我不想改變它來添加編譯指令。 – 2009-12-16 16:27:12