SQLite级联删除

时间:2013-06-26 14:19:47

标签: c# database sqlite windows-runtime

实际上我正在使用SQLite DataBase开发windows metro应用程序。我使用sqlite manager(mozilla)进行管理。我尝试删除级联,但它只适用于sqlite管理器而不是C#代码:

我的功能


public async Task<string> DeleteSurvey(int SurveyID)
{
    string result = string.Empty;
    var db = new SQLite.SQLiteAsyncConnection(App.DBPath);
    var survey = await GetSurvey(SurveyID);
    var res = await db.DeleteAsync(survey);

    if (res > 0)
        result = "Success";
    else
        result = "Echec";

    return result;
}

db.CreateTable<Survey>();

SQLiteCommand command1 = new SQLiteCommand(db);
command1.CommandText = "create table if not exists SurveyItemGroup";
command1.CommandText += "(ID integer primary key autoincrement not null, IDSurvey integer,";
command1.CommandText += "Number integer, Name varchar(50), FOREIGN KEY(IDSurvey) REFERENCES Survey(ID) ON DELETE CASCADE ON UPDATE CASCADE)";
command1.ExecuteNonQuery();

在C#代码中,它只删除调查表而不是两者(Survey和SurveyItemGroup)

PS:我对pragma(pragma foreign_keys=ON;)有同样的问题,只有当我使用sqlite manager时它才有效。

1 个答案:

答案 0 :(得分:5)

(我正在添加这个答案,因为OP将此视为他的解决方案,但只是在评论中。)

目前,要使ON DELETE CASCADE正常工作,必须在每个新连接中发出pragma foreign_keys=on;。它是not currently一个持久的设置。