question

JoeyIreland avatar image
0 Votes"
JoeyIreland asked ·

SQlite Extensions One To One with list sub class

Hi,

I have a class I have made with a sub class which is a list but when i use the OneToOne extension tag it is inserting it as null am I missing something?

my class:

 public class RAMs
     {
         [PrimaryKey, AutoIncrement]
         public Int32 Id { get; set; }
         public String ProjectName { get; set; }
    
         [OneToOne]
         public List<Hazard> Hazards { get; set; }
     }
    
 public class Hazard
     {
         public String HazardName { get; set; }
     }


and when i save into the local db i am using this snippet:

 if (App.RamsDatabase.InsertRAMs(m))
             {
                 await Navigation.PushAsync(new Edit(m.Id));
             }
    
 public Boolean InsertRAMs(RAMs model)
         {
             lock (locker)
             {
                 database.InsertWithChildren(model);
    
                 return true;
             }
         }
dotnet-xamarinformsdotnet-sqlite
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

when i use the OneToOne extension tag it is inserting it as null am I missing something

The One-To-One extension is a one-way relationship. An example could be vehicle and registration certificate – each vehicle has one and only one registration certificate, and one registration certificate is associated with one and only one vehicle. It's not suitable for your needs.

Try using One-to-many extension instead. One-to-many relationships are used in general for parent-children or whole-elements relations.

public class RAMs
{
    [PrimaryKey, AutoIncrement]
    public Int32 Id { get; set; }
    public String ProjectName { get; set; }

    [OneToMany]
    public List<Hazard> Hazards { get; set; }
}

You could google with the keyword as SSQLite-Net Extensions – one-to-one relationships and SQLite-Net Extensions – one-to-many relationships to check the related documentation.

Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JarvanZhang-MSFT thanks for the reply. When changing to onetomany that seems fine but when saving into the local db its still saying null. so I think the issue lays with this section

 lock (locker)
              {
                  database.InsertWithChildren(model);
        
                  return true;
              }
0 Votes 0 ·

There may be something wrong with the saving function code, please check if the items have been added to the database successfully. And post the related code, it will help to get the cause.

0 Votes 0 ·