我正在使用带有实体的MVC 3,现在我已经使用了我的控制器中的以下代码行
using (var scope = new TransactionScope())
{
_myRepository.DeleteFM1(id);
_myRepository.DeleteFM2(id, name);
scope.Complete();
}
Run Code Online (Sandbox Code Playgroud)
在我的DeleteFM2方法中,恰好是我在Entity类中定义的方法,如下所示:
public void DeleteFM2(int id, string name)
{
var data= _repositoryMD.Fetch().Where(mColl => mColl.Col1 == id);
if (data!= null)
{
//insert here is giving some error MSDTC error !
// here I prepare a message using the '**data**'
_repositoryHistory.Insert(name, message, "FM2", "Delete", dateTime);
_repositoryMD.Attach(data);
_repositoryMD.Delete(data);
_repositoryMD.SaveChanges();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个单独的类,我已将Insert方法定义为
public bool Insert(string realName, string logMessage, string tableName, string changeType, DateTime dateTime)
{
var history …Run Code Online (Sandbox Code Playgroud) 在我们当前的项目中,我们使用ADO.NET Entity Framework作为应用程序的数据层.有些任务需要在事务中运行,因为在数据库中有很多工作要做.我正在使用TransactionScope来包围这些任务.
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
// Do something...
transactionScope.Complete();
}
Run Code Online (Sandbox Code Playgroud)
问题是我在使用TransactionScope时发生异常:
System.Data.EntityException:基础提供程序在Open上失败.---> System.Transactions.TransactionManagerCommunicationException:与底层事务管理器的通信失败.---> System.Runtime.InteropServices.COMException(0x80004005):错误HRESULT E_FAIL已从调用COM组件返回.
似乎此错误必须与MSDTC(Microsoft分布式事务处理协调器)执行某些操作.当我更改MSDTC的安全配置时,会抛出另一个异常:
System.Data.EntityException:基础提供程序在Open上失败.---> System.Transactions.TransactionManagerCommunicationException:已禁用分布式事务管理器(MSDTC)的网络访问.请使用组件服务管理工具在MSDTC的安全配置中启用DTC以进行网络访问.
但是,MSDTC已配置,TransactionScope将导致错误.有人知道这里出了什么问题吗?