我的数据库中有两张表,一张用于食谱,一张用于配料。当一个特定的食谱被删除时,我希望它的所有成分也消失。我已经声明了与级联属性集的一对多关系,但是当我删除一些食谱时,它不会删除相关成分。
这是我的表:
public class Recipe_Model
{
[PrimaryKey AutoIncrement]
public int RecipeID { get; set; }
public string RecipeName { get; set; }
public double RecipeCost { get; set; }
public double ServingsNo { get; set; }
public double CostPercent { get; set; }
public double SellingPrice { get; set; }
public double CostPerServing { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)] // One to many relationship with Ingredients
public ObservableCollection<Ingredients_Model> Ingredients { get; set; }
}
public class Ingredients_Model
{
[PrimaryKey AutoIncrement]
public int IngredientID { get; set; }
[ForeignKey(typeof(Recipe_Model))]
public int RecipeID { get; set; }
public string IngredientName { get; set; }
public string UsedUnit { get; set; }
public string PurchasedUnit { get; set; }
public double QuantityUsed { get; set; }
public double QuantityPurchased { get; set; }
public double PurchasePrice { get; set; }
public double IngredientCost { get; set; }
}
这是我的删除操作:
public void DeleteRecipe()
{
using (SQLiteConnection database = DependencyService.Get<ISQLite>().GetConnection())
{
var recipe = database.Get<Recipe_Model>(RecipeID);
database.Delete(recipe, true);
}
}
我究竟做错了什么?