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();
}
代码抛出“连接对象不能在事务范围中登记”
SQL CE 4是否支持TransactionScope?如果没有,是否有任何解决方法,所以我可以安全地删除对象?
答案 0 :(得分:4)
如果SQL CE不支持事务范围,您肯定可以使用正常的事务方法,connection.BeginTransaction然后是transaction.Commit或Rollback ...
答案 1 :(得分:1)
如果在事务范围之外打开连接,则需要显式调用EnlistTransaction。您不能在SQL CE中隐式使用与事务作用域的连接,如here
所述