LinqToSql overrides insertion with C # inner classes

I have a base recipe class and I am using datacontext. I have tried the insert method for a recipe in datacontext and am trying to insert it into its children. Nomatter what i do i cant get the child to insert. Currently only recipe inserts and nothing happens to the child.

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

      

I've tried everything I can think of. I even created another datacontext in the method, and after the instance came back from ExecuteDynamicInsert with an ID, tried adding it and I get timeout errors.

+1


source to share


2 answers


I understood that. Override SubmitChanges in the DataContext and find all inserts and updates that are recipes. Run the algorithm to add children there.



    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


source


The InsertX / UpdateX / DeleteX methods are called after LINQ to SQL has determined which objects will be included in SubmitChanges.

You would get a timeout using two DataContext, most likely due to locking issues between the two transactions.



I cannot clearly understand what you are trying to achieve here - usually LINQ to SQL manages the child relationships themselves - what are PlainIngredients and TextIngredients?

+1


source







All Articles