Error on using TransactionScope in EF4 & SQL Compact 4

时光毁灭记忆、已成空白 提交于 2019-12-29 09:27:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!