I'm having a problem with my DataContext. In the following code snippet, I first delete existing objects in my datacontext, and later in same code, adding new again. The problem is, since I'm using .DeleteOnSubmit() its still containing 2 entities in my Datacontext, tho its stored correctly in my Database.
Here the code:
public void ConnectedApartment(Apartment newApartment, Apartment existing)
{
ConApart ConApartTemp = new ConApart();
ConApartTemp.Apartment1 = newApartment;
ConApartTemp.Apartment = existing;
DatabaseConnection.ConAparts.InsertOnSubmit(ConApartTemp);
}
public void opdaterConApart(ArrayList connected, Apartment Apartmenten)
{
foreach (ConApart sam in Apartmenten.ConAparts)
{
DatabaseConnection.ConAparts.DeleteOnSubmit(sam);
}
foreach (ConApart sam2 in Apartmenten.ConAparts1)
{
DatabaseConnection.ConAparts.DeleteOnSubmit(sam2);
}
foreach (Apartment apa in connected)
{
ConnectedApartment(apa, Apartmenten);
}
DatabaseConnection.SubmitChanges();
}
If I doesn't SubmitChanges() of the delete before
foreach (Apartment apa in connected)
{
ConnectedApartment(apa, Apartmenten);
}
Then my Datacontext will contain 2 entities, and my gui will show duplicates for each time its changing.
If I use following:
public void ConnectedApartment(Apartment newApartment, Apartment existing)
{
ConApart ConApartTemp = new ConApart();
ConApartTemp.Apartment1 = newApartment;
ConApartTemp.Apartment = existing;
DatabaseConnection.ConAparts.InsertOnSubmit(ConApartTemp);
}
public void opdaterConApart(ArrayList connected, Apartment Apartmenten)
{
foreach (ConApart sam in Apartmenten.ConAparts)
{
DatabaseConnection.ConAparts.DeleteOnSubmit(sam);
}
foreach (ConApart sam2 in Apartmenten.ConAparts1)
{
DatabaseConnection.ConAparts.DeleteOnSubmit(sam2);
}
DatabaseConnection.SubmitChanges();
foreach (Apartment apa in connected)
{
ConnectedApartment(apa, Apartmenten);
}
DatabaseConnection.SubmitChanges();
}
then it works, but that solution is weak for Rollback function, since it going to: 1. Delete existing objects 2. submitChanges 3. Add new objects 4. submitChanges
But if something fails between point 2 and 3, the objects that are deleted is lost. Any suggestions? How can I maintain my context, so it doesn't hang with double entities, and still handle Rollback functionality if something goes wrong???