0

데이터베이스에 두 개의 테이블이 있습니다. 하나는 요리법 용이고 다른 하나는 재료 용입니다. 특정 레시피가 삭제되면 모든 성분도 사라지게됩니다. 캐스케이드 속성이 설정된 일대 다 관계를 선언했지만 일부 레시피를 삭제하면 관련 재료가 삭제되지 않습니다.계단식 삭제가 작동하지 않습니다.

여기 내 테이블은 다음과 같습니다이 내 삭제 작업입니다

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

내가 무슨 일을하고 있는가?

답변

1

캐스케이드 조작은 메모리에있는 오브젝트에 대해서만 작동합니다. 특정 시나리오에서 Get 메서드를 통해 데이터베이스에서 단일 개체를 가져오고 계단식 작업은 모든 메모리 내 관계를 삭제합니다. 이는 Ingredients 속성이 null이기 때문에 현재 아무것도 아닙니다.

// This would work as it loads children to memory, but it's inefficient 
var recipe = database.GetWithChildren<Recipe_Model>(RecipeID); 
database.Delete(recipe, true); 

대신 : 이미 메모리에 객체가없는 경우, 그것은을로드 할 수 이해가되지 않습니다

단지 계단식 삭제가 무엇을 정확히 인 삭제하는 식별자를 얻을 수 수동으로 삭제하는 것이 좋습니다.

database.Execute("DELETE FROM [Ingredients_Model] WHERE [RecipeID] == ?", recipe.Id); 
database.Delete(recipe); 
+0

현재 메모리에있는 개체를 삭제하는 방법은 무엇입니까? 또한 내가이 권한을 얻는다면, 현재 조작하고있는 객체가 메모리에 저장 될 것입니다. 그렇죠? 이전에이 작업을 수행했습니다. database.Delete (RecipeID); 하지만 작동하지 않았으므로 Get 메서드로 변경하여 테스트 해 보았습니다. 이 쿼리는 먼저 메모리에서 객체를 가져 옵니까? – Tehreem

+0

아, 그것을 해결했습니다. 그리고 메모리 문제의 모든 개체를 잡았어! 감사. – Tehreem