0

Please I want a solution for this scenario (with code please):

  1. I have two entity (X and Y) loaded by the same DbContext
  2. Load entity X
  3. Modified entity X
  4. Load entity Y
  5. Modified entity Y
  6. Save changes for entity Y
  7. Undo changes for entity X

Every time when I save change for entity Y, it will also apply to the entity X

my example code :

1- Load and Update the client X :

public static void UpdateClient(ModeleContainer GStock, Modele.Client Client)
        {
            try
            {
                    using (frmClient frmClient = new frmClient(GStock, Client))
                    {
                        if (frmClient.ShowDialog() == DialogResult.Cancel)
                        {
                            GStock.UndoDbContext();
                        }
                        else
                        if (frmClient.ShowDialog() == DialogResult.OK)
                            GStock.SaveChanges();
                        }
                    }
            }
            catch (Exception e)
            {
                Methods.DealWithException(e);
            }
        }

2- Load and Update the category Y (called by the frmClient form's)

public static void UpdateCategorie(ModeleContainer GStock, Modele.Category Category)
        {
            try
            {
                    using (frmCategorie frmCategorie = new frmCategorie(GStock, Category ))
                    {
                        if (frmCategorie.ShowDialog() == DialogResult.OK)
                        {
                            // CategorieBindingSource is a public BindingSource
                            frmCategorie.CategorieBindingSource.EndEdit();
                            GStock.SaveChanges();
                        }
                        else
                            GStock.UndoDbContext();
                    }
            }
            catch (Exception e)
            {
                Methods.DealWithException(e);
            }
        }

3- GStock is a shared DbContext for all forms, to track the changes

cheikh17
  • 57
  • 8

0 Answers0