问题
using (TransactionScope scope = new TransactionScope())
using (var context = new dwfEntities())
{
var field = (from x in context.DynFields where x.Id == id select x).First();
//delete defaults
foreach (var item in from x in context.DynFieldDefaults where x.DynField_Id == id select x)
{
context.DeleteObject(item);
}
context.SaveChanges();
//delete field
context.DeleteObject(field);
context.SaveChanges();
//commit
scope.Complete();
}
The code throws "The connection object can not be enlisted in transaction scope"
Does SQL CE 4 support TransactionScope ? if not, is there any workaround so I can safely delete objects ?
回答1:
in case SQL CE does not support transaction scope, you can surely use the normal transactional approach, connection.BeginTransaction then transaction.Commit or Rollback...
回答2:
If the connection is opened outside of the transaction scope you need to explicitly call EnlistTransaction. You cannot implicitly use a connection with a transaction scope in SQL CE as described here
来源:https://stackoverflow.com/questions/5153573/error-on-using-transactionscope-in-ef4-sql-compact-4