c# - TextBlob 为空

标签 c# xamarin sqlite-net-extensions

我是第一次使用 SQLite,我正在做一个学生项目,我做了很多研究,但我无法解决我的问题。我正在尝试使用 sqlite-net-extensions,但我的 TextBlob 始终为空。如何使用 WithChilder 的方法,我也无法让它们工作

这是我的模型类:

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

这是我的数据库:

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

}

最佳答案

您还需要使用 SQLiteExtensions 的函数 InsertWithChildren

像这样更改函数 SaveFoodNutrientAsync:

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

这会将 TextBlob 更新为它对应的值。 您还需要使用 GetAllWithChildren 来获取您的对象 with blob。

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

关于c# - TextBlob 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50434283/

相关文章:

xamarin - 'Microsoft.EntityFrameworkCore.Query.ResultOperators.Internal.TrackingExpressionNode' 的类型初始值设定项引发异常

c# - 在 Xamarin 中,我是否应该担心在 ScrollView 中显示大量内容?

c# - 如何将外键映射到 SQLite-Net Extensions 中的对象字段?

c# - SQLite Xamarin 中的一对多关系

c# - WCF/ASP.NET - 防止滥用,例如 DOS

listview - 如何将 ListView 中元素的索引绑定(bind)到第一个 ListView 内第二个 ListView 的 ItemsSource 中?

c# - 创建和解析自定义扩展文件 c#

c# - 如何使用表中的嵌套类和列表?

c# - MVVM:VM 对象应该直接公开 M 对象,还是仅通过委托(delegate)给 M 的 getter 的 getter 公开?

c# - 在 C# 中计算 Cron 下一次运行时间