Please I want a solution for this scenario (with code please):
- I have two entity (X and Y) loaded by the same DbContext
- Load entity X
- Modified entity X
- Load entity Y
- Modified entity Y
- Save changes for entity Y
- 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