简体   繁体   中英

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. I followed the documentation on: 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? 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.

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.

any ideas?

It works, make sure the object you pass to SQLiteNetExtensions.Extensions.WriteOperations.DeleteAll(..) function has all the child objects present in it.

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

Better if you can get the Missions objects like this

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

I was using singleton instance of SQLiteAsyncConnection in Xamarin.Forms, but anyways had to call "PRAGMA foreign_keys=ON;" before each DeleteAsync call . Calling it once after opening the connection didn't work for me. No need for SQLiteNetExtensions library to overcome CASCADE DELETION "issue"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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