SQLite extension update related tables (error no primary key)

Marco_SeraphinMarco_Seraphin DEUniversity ✭✭
edited September 2015 in Cross Platform with Xamarin

Hi all, hi Guillermo,

I am using SQLite with the SQLite extensions.

I have read all the docs and forum threads like this:

and this sample https://bitbucket.org/twincoders/sqlite-net-extensions#branches

BUT I always get the error:

OneToMany relationships require Primary Key in the destination entity

If I call the method:


I also tried the extension method

SQLiteNetExtensions.Extensions.WriteOperations.InsertAllWithChildren(dbConn, myList, true);

BUT I have defined an Primary Key in both entity classes (master and detail).

Here are my classes:

 public class DBBucketEntitiy
       [PrimaryKey, AutoIncrement]
       public int Id { get; set; }

       [MaxLength(250), Unique]
       public string Name { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead | CascadeOperation.CascadeDelete)]     
    public List<BucketPhotoItem> PhotoItemList { get; set; }

public class DBBucketPhotoEntity
       [PrimaryKey, AutoIncrement]
       public int Id { get; set; }

    [ForeignKey(typeof(DBBucketEntitiy))]     // Specify the foreign key
    public int DBBucketEntitiyId { get; set; }

        public string FileName { get; set; }

And here my Insert Code:

dbConn = new SQLiteConnection (sqlitePlatform, dbPath,false);


foreach (var photoItem in bucketItem.BucketPhotoItemList)
          result = dbConn.Insert(new DBBucketPhotoEntity
                  FileName = photoItem.FileName,
                  Latitude = photoItem.Latitude,
                  Longitude = photoItem.Longitude,
                  DateTimeOfPhoto = photoItem.DateTimeOfPhoto

var newBucketItem = new DBBucketEntitiy() 
        Name = bucketItem.Name,
        PhotoItemList = bucketItem.BucketPhotoItemList

 List<DBBucketEntitiy> DBBucketItemList = new List<DBBucketEntitiy>();

//SQLiteNetExtensions.Extensions.WriteOperations.InsertAllWithChildren(dbConn, DBBucketItemList, true);

dbConn.UpdateWithChildren(newBucketItem); // Here the Primary key error occurs !!!!!

Any error on my side ? Any idea what I should change ?

Thank you in advance



  • Marco_SeraphinMarco_Seraphin DEUniversity ✭✭

    Hi all,

    found my error.

    Instead of:

    [OneToMany(CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead | CascadeOperation.CascadeDelete)]     
    public List<BucketPhotoItem> PhotoItemList { get; set; }

    I have to use my database entity:

    [OneToMany(CascadeOperations = CascadeOperation.CascadeInsert | CascadeOperation.CascadeRead | CascadeOperation.CascadeDelete)]     
    public List<**DBBucketPhotoEntity**> PhotoItemList { get; set; }

    I had a database entity class and an object class and mapped this....but for the foreign key reaction stuff I have to use the database entity class.


  • rzee7rzee7 INUniversity ✭✭✭✭✭
    edited September 2015

    Hello @MarcoSeraphin

    Thank you for posting your answer :smile: Its always appreciated when someone like you finish own created post :smile:


Sign In or Register to comment.