1

我在 Windows Store App 的隔离存储中有 SQLite 数据库。
我使用SQLite for Windows Runtime
当我想删除所有表条目时,我使用以下方法:

  public static async void DeleteAllProjects()
    {
        var storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("myDb.sqlite");
        using (var db = new SQLiteConnection(storageFile.Path))
        {
            try
            {
                db.DeleteAll<Projects>();
            }
            catch
            (Exception ex)
            {
                Debug.WriteLine("Delete error   " + ex.Message);
            }
        }
    }

所有的工作都像一个魅力。
但是当我需要删除部分条目时:

 public static async void Delete(List<Projects> projects)
    {
        var storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("myDb.sqlite");
        using (var db = new SQLiteConnection(storageFile.Path))
        {
            try
            {
                foreach (var project in projects)
                {
                    var existingProject = (db.Table<Projects>().Where(
                        p => p.id == project.Id)).FirstOrDefault();
                    if (existingProject != null)
                    {
                          db.Delete<Projects>(existingProject);
                    }
                }
            }
            catch
            (Exception ex)
            {
                Debug.WriteLine("Delete error   " + ex.Message);
            }
        }
    }

我处理了例外

ex.Message = "Cannot delete Projects: it has no PK" string

有人可以帮助我吗?

4

0 回答 0