我是第一次使用 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/