简体   繁体   中英

TextBlob is null

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>();
    }

}

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 the blob. 对象的对象。

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

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