3

I am working with SQLite for the first time, I have working on a student project, and I have done a lot of research, but I cannot fix my problem. I am trying to use sqlite-net-extensions, but my TextBlob is always null. How can use the methods with WithChilder, I cannot get them to work either

This is my model class:

    public class FoodNutrient
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string foodname { get; set; }
    public string sd { get; set; }
    public string time { get; set; }
    [TextBlob(nameof(nutrientsBlobbed))]
    public List<NutrientItem> nutrients { get; set; }
    public string nutrientsBlobbed { get; set; }
}

public class NutrientItem
{
    public string name { get; set; }
    public string group { get; set; }
    public string unit { get; set; }
    public double value { get; set; }
}

This is my database:

    public class FoodDatabase
{
    readonly SQLiteAsyncConnection database;

    public FoodDatabase(string dbPath)
    {
        database = new SQLiteAsyncConnection(dbPath);

        database.CreateTableAsync<FoodNutrient>().Wait();
    }

    public Task<int> SaveFoodNutrientAsync(FoodNutrient foodNutrient)
    {
        return database.InsertAsync(foodNutrient);
    }

    public Task<List<FoodNutrient>> GetFoodNutrientsAsync()
    {
        return database.Table<FoodNutrient>().ToListAsync();
        //return database.GetAllWithChildren<FoodNutrient>();
    }

}
DoubleD
  • 148
  • 10
  • would You consider not using that driver at all? SQL lite is fully supported with entity framework, so do You really need to spend your time that close to the metal so to speek? https://learn.microsoft.com/en-us/ef/core/providers/sqlite/?tabs=dotnet-core-cli – T. Nielsen Jun 22 '22 at 08:51
  • try to add attributes like: [OneToMany(CascadeOperations = CascadeOperation.All)] and [ManyToOne] – Serhii Matvienko Jun 23 '22 at 11:27

1 Answers1

-1

You also need to use the function InsertWithChildren of the SQLiteExtensions.

Change the function SaveFoodNutrientAsync like this:

public Task<int> SaveFoodNutrientAsync(FoodNutrient foodNutrient)
{
    return database.InsertWithChildrenAsync(foodNutrient);
}

This will update the TextBlob to the value it corresponds. You will also need to use the GetAllWithChildren to get your object with the blob.

public Task<List<FoodNutrient>> GetFoodNutrientsAsync()
{
    return database.GetAllWithChildren<FoodNutrient>();
}