Save order

Posts   
 
    
cratchit
User
Posts: 1
Joined: 12-Jan-2005
# Posted on: 12-Jan-2005 16:44:07   

Quick question... I had some strange problems with ordering of ingredients in a recipe app that I'm building. The first item defined was always displaying last. It's a bit of a hack at the moment, but I'm sorting by ingredient ID, an auto-increment integer field.

Here's how I was saving when the problem was occurring...


RecipeIngredientCollection tempIngs = tempIngredients;
foreach (RecipeIngredientEntity ing in tempIngs)
{
    RecipeIngredientEntity newIng = new RecipeIngredientEntity();
    newIng.RecipeID = recipe.ID;
    newIng.Ingredient = ing.Ingredient;
    newIng.Quantity = ing.Quantity;
    newIng.Measurement = ing.Measurement;
    newIng.Note = ing.Note;
        recipe.Ingredients.Add(newIng)
}
recipe.Ingredients.SaveMulti(true);

And here's how I'm saving now. Not quite as efficient or safe, but I'm not sure what else to try.


RecipeIngredientCollection tempIngs = tempIngredients;
foreach (RecipeIngredientEntity ing in tempIngs)
{
    RecipeIngredientEntity newIng = new RecipeIngredientEntity();
    newIng.RecipeID = recipe.ID;
    newIng.Ingredient = ing.Ingredient;
    newIng.Quantity = ing.Quantity;
    newIng.Measurement = ing.Measurement;
    newIng.Note = ing.Note;
    newIng.Save();
}

Can anyone explain what was happening previously? Thanks all!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 12-Jan-2005 17:51:28   

It should save the newIng entities added to Ingredients in the same order as they're added, as SaveMulti() simply walks the collection from front to back for dirty entities and saves them, however, as you save recursively, the first newIng is first saving its parent, as it depends on that entity. That entity is not new, so it is skipped but in there the related entities depending on it are saved. Though, these should be saved in the same order.

Is the order completely random or are they backwards?

Frans Bouma | Lead developer LLBLGen Pro