SQL CE 4 System.Transaction支持 [英] SQL CE 4 System.Transaction support
问题描述
类似的问题被问到这里,但没有答案。
A similar question was asked here but had no answer.
我正在尝试使用与EF CTP4和SQL CE 4的System.Transactions.CommittableTransaction。
I am attempting to use a System.Transactions.CommittableTransaction with EF CTP4 and SQL CE 4.
我为我的ASP.NET MVC控制器操作创建了以下事务属性:
I have created the following transaction attribute for my ASP.NET MVC Controller actions:
public class TransactionAttribute : ActionFilterAttribute
{
CommittableTransaction transaction;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
transaction = new CommittableTransaction();
Transaction.Current = transaction;
base.OnActionExecuting(filterContext);
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
try
{
var isValid = filterContext.Exception == null || filterContext.ExceptionHandled;
if (filterContext.Controller.ViewData.ModelState.IsValid && isValid) {
transaction.Commit();
} else {
transaction.Rollback();
Transaction.Current = null;
}
}
finally
{
transaction.Dispose();
}
}
}
当我使用此过滤器得到错误:
When I use this filter I get the error:
System.InvalidOperationException:连接对象不能被登记在事务范围中。
System.InvalidOperationException: The connection object can not be enlisted in transaction scope.
然而,以下测试通过:
[Test]
public void Transaction_rolls_back_if_exception()
{
var transaction = new CommittableTransaction();
Transaction.Current = transaction;
try
{
var project = new Project { Title = "Test" };
projectRepo.SaveOrUpdate(project);
context.SaveChanges();
var post = new Post { Title = "Some post" };
blogRepo.SaveOrUpdate(post);
throw new Exception();
context.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
Transaction.Current = null;
}
projectRepo.GetAll().Count().ShouldEqual(0);
blogRepo.GetAll().Count().ShouldEqual(0);
}
有什么关系到我如何初始化DbContext?
Has this something to do with how I am initializing the DbContext?
推荐答案
我遇到了同样的问题。您不能与CE连接使用事务。最后我的数据文件管理我的ce连接,并实现一个工作模式单元来保存我的操作,然后在SqlCeTransaction中执行所有计划的操作,然后调用commit或rollback。
I ran into this same issue. You cannot use Transactions with the CE Connection. I ended up having my datacontext manage my ce connection and implementing a unit of work pattern to hold my actions, then executing all the scheduled actions inside a SqlCeTransaction then calling commit or rollback myself.
http://msdn.microsoft.com/en-us/library/ csz1c3h7.aspx
这篇关于SQL CE 4 System.Transaction支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!