0
我在我的數據庫中有兩個表,一個用於配方,另一個用於他們的配料。當一個特定的食譜被刪除時,我希望它的所有成分都消失了。我已經聲明瞭與cascade屬性集的一對多關係,但是當我刪除一些配方時,它並沒有刪除相關的成分。刪除級聯不工作
這裏是我的表:
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);
}
}
我在做什麼錯?
我該如何去刪除當前在內存中的對象?此外,如果我正確地掌握了這一點,那麼我目前操縱的對象將會記憶中,對嗎?我以前是這樣做的:database.Delete(RecipeID);但由於它無法正常工作,我將其更改爲Get方法來嘗試。這個查詢是否也首先從內存中獲取對象? –
Tehreem
啊工作了。在記憶問題中得到了所有的對象!謝謝。 – Tehreem