Per impostazione predefinita, SaveChanges eseguirà in una transazione (vedere la Remarks part nella documentazione)
Se si desidera un maggiore controllo sulla transazione, è possibile avvolgere le SaveChanges blocco in un TransactionScope. SaveChanges raccoglierà quindi la transazione ambientale e userà quella.
Questo può essere utile quando si desidera una transazione distribuita (ad esempio con più contesti o se si utilizza WCF).
Come lei ha ricordato che si utilizzano diversi modelli, è necessario utilizzare entrambi i ObjectContexts entro un TransactionScope (e utilizzare some logic con AcceptAllChanges)
Il tuo codice sarebbe quindi simile:
using (TransactionScope scope = new TransactionScope())
{
//Do something with context1
//Do something with context2
//Save Changes but don't discard yet
context1.SaveChanges(false);
//Save Changes but don't discard yet
context2.SaveChanges(false);
//if we get here things are looking good.
scope.Complete();
//If we get here it is save to accept all changes.
context1.AcceptAllChanges();
context2.AcceptAllChanges();
}
fonte
2011-12-28 09:40:49
Yup, ho la mia risposta, grazie :) –