2008-12-13 117 views
1

我有一個基本的配方類,我正在使用一個datacontext。我覆蓋了datacontext中配方的插入方法,並試圖插入到它的子項中。無論我做什麼我都無法讓孩子插入。目前,只有配方插入,孩子沒有任何反應。LinqToSql重寫插入與內部類C#

partial void InsertRecipe(Recipe instance) 
    { 
     // set up the arrays 
     for (int x = 0; x < instance.PlainIngredients.Count; ++x) 
     { 
      instance.TextIngredients.Add(new TextIngredient() 
      { 
       StepNumber = x + 1, 
       Text = instance.PlainIngredients[x] 
      }); 
     } 

     this.ExecuteDynamicInsert(instance); 
    } 

我試過了我能想到的一切。我甚至在該方法中實例化了另一個datacontext,並且在實例從ExecuteDynamicInsert帶上id後試圖添加它,並且出現超時錯誤。

回答

1

我想通了。覆蓋DataContext中的SubmitChanges,並查找所有配方的插入和更新。運行該算法以在那裏添加子項。

public override void SubmitChanges(
     System.Data.Linq.ConflictMode failureMode) 
    { 
     ChangeSet changes = this.GetChangeSet(); 

     var recipeInserts = (from r in changes.Inserts 
         where (r as Recipe) != null 
         select r as Recipe).ToList<Recipe>(); 

     var recipeUpdates = (from r in changes.Updates 
         where (r as Recipe) != null 
         select r as Recipe).ToList<Recipe>(); 

     ConvertTextData(recipeInserts); 
     ConvertTextData(recipeUpdates); 

     base.SubmitChanges(failureMode); 
    } 
1

在LINQ to SQL確定將在SubmitChanges中包含哪些對象之後調用InsertX/UpdateX/DeleteX方法。

由於兩個事務之間存在鎖定問題,您可能會因使用兩個DataContext而發生超時。

我無法清楚地確定你在這裏實現的目標 - 通常LINQ to SQL管理子關係 - 什麼是PlainIngredients和TextIngredients?