简体   繁体   English

xamarin.forms 的 SQLite 扩展:删除级联不起作用,但代码编译没有错误

[英]SQLite extension for xamarin.forms : delete on cascade not working, but the the code compiles with no errors

I'm using SQLiteNetExtensions to create a foreign key from one table to another so I can delete on cascade.我正在使用 SQLiteNetExtensions 创建从一个表到另一个表的外键,以便我可以在级联上删除。 I followed the documentation on: https://bitbucket.org/twincoders/sqlite-net-extensions ,我遵循了以下文档: https://bitbucket.org/twincoders/sqlite-net-extensions

but when I delete a record on my Missions Table, the records on my Locations Table that reference that primary key don't get deleted,但是当我删除任务表上的记录时,位置表中引用该主键的记录不会被删除,

am I missing something?我错过了什么吗?

These are my tables:这些是我的表:

 using SQLiteNetExtensions.Attributes;

 public class Locations 
{
    [PrimaryKey, AutoIncrement]
    public int locationId { get; set; }

    [ForeignKey(typeof(Missions))]
    public int missionId { get; set; }

}

and :和 :

using SQLiteNetExtensions.Attributes;

public class Missions
{
    [PrimaryKey, AutoIncrement]
    public int missionId { get; set; }

    public String missionName { get; set; }


    [OneToMany(CascadeOperations = CascadeOperation.CascadeDelete)]
    public List<Locations> Locations { get; set; }
}

Also this is the delete method used to delete records from the missions table:这也是用于从任务表中删除记录的删除方法:

 public Task<int> DeleteMyMissionAsync(Missions myMission)
    {
        return database.DeleteAsync(myMission);
    }

Is there any way to check what this attributes from the SQLite extension are doing?有什么方法可以检查 SQLite 扩展中的这个属性在做什么? my code compiles and runs, so I don't know where the mistake is.我的代码编译并运行,所以我不知道错误在哪里。

I tried also adding "using SQLiteNetExtensionsAsync" but this didn't make any difference.我还尝试添加“使用 SQLiteNetExtensionsAsync”,但这没有任何区别。

EDIT: Changed my delete method to :编辑:将我的删除方法更改为:

public Task DeleteMyMissionAsync(Models.MyCreatedMissions myMission)
    {

        return database.DeleteAsync(myMission, recursive: true); 

    } 

it runs but when I delete a record in the Missions table referenced by my Locations table, it only deletes that record and all the records in the Locations table that reference that record still exist.它运行但是当我删除我的 Locations 表引用的 Missions 表中的一条记录时,它只会删除该记录并且 Locations 表中引用该记录的所有记录仍然存在。

any ideas?有任何想法吗?

It works, make sure the object you pass to SQLiteNetExtensions.Extensions.WriteOperations.DeleteAll(..) function has all the child objects present in it.它有效,请确保您传递给 SQLiteNetExtensions.Extensions.WriteOperations.DeleteAll(..) 函数的对象中包含所有子对象。

Call should be like this调用应该是这样的

SQLiteNetExtensions.Extensions.WriteOperations.DeleteAll(db, new Missions[] { missionsObj}, true);

In this missionsObj.Locations must not be null and count not zero在此任务中,Obj.Locations 不能为空且计数不能为零

Better if you can get the Missions objects like this如果你能得到这样的 Missions 对象就更好了

SQLiteNetExtensions.Extensions.ReadOperations.GetAllWithChildren<Missions>(db, null, true);

so it will get all its children, so you can pass this mission objects to the above delete method.所以它会得到它所有的孩子,所以你可以把这个任务对象传递给上面的删除方法。 and It should work它应该工作

PS: assuming all the attributes for the one to many relationships are set properly PS:假设一对多关系的所有属性都设置正确

I was using singleton instance of SQLiteAsyncConnection in Xamarin.Forms, but anyways had to call "PRAGMA foreign_keys=ON;"我在 Xamarin.Forms 中使用 SQLiteAsyncConnection 的单例实例,但无论如何不得不调用“PRAGMA foreign_keys=ON;” before each DeleteAsync call .在每次 DeleteAsync 调用之前 Calling it once after opening the connection didn't work for me.打开连接后调用一次对我不起作用。 No need for SQLiteNetExtensions library to overcome CASCADE DELETION "issue"不需要 SQLiteNetExtensions 库来克服级联删除“问题”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM