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:
http://forums.xamarin.com/discussion/18768/newbie-how-to-use-sqlite-net-extensions-and-foreign-keys

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:

dbConn.UpdateWithChildren(myObject);

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:

[Table("DBBucketEntitiy")]
 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; }
 }

[Table("BucketPhotoItem")]
public class DBBucketPhotoEntity
  {
       [PrimaryKey, AutoIncrement]
       public int Id { get; set; }

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

    [Column("FileName")]
        public string FileName { get; set; }
 }

And here my Insert Code:

dbConn = new SQLiteConnection (sqlitePlatform, dbPath,false);
dbConn.CreateTable<DBBucketEntitiy>();
dbConn.CreateTable<DBBucketPhotoEntity>();

....

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>();
 DBBucketItemList.Add(newBucketItem);

//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

Answers

  • 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.

    Marco

  • 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:

    Cheers!!
    RIYAZ

Sign In or Register to comment.