我正在寻找插入Entity Framework的最快方法.
我问这个是因为你有一个活跃的TransactionScope并且插入很大(4000+).它可能持续超过10分钟(事务的默认超时),这将导致事务不完整.
我只是想知道使用Serializable作为默认Isolationlevel的一个很好的理由可能是在创建System.Transactions TransactionScope时,因为我想不出任何(并且似乎你不能改变默认值,web/app.config所以你总是要设置它你的代码)
using(var transaction = TransactionScope())
{
... //creates a Transaction with Serializable Level
}
Run Code Online (Sandbox Code Playgroud)
相反,我总是要写这样的样板代码:
var txOptions = new System.Transactions.TransactionOptions();
txOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
using(var transaction = new TransactionScope(TransactionScopeOption.Required, txOptions))
{
...
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我已经构建了一个类来同步两个不同数据源之间的数据.此同步分为多个部分(和方法).每个方法都有自己的TransactionScope,方法按顺序运行.
每次运行此代码时,我都会收到以下错误消息:
"与当前连接关联的事务已完成但尚未处理.事务必须在连接可用于执行SQL语句之前处理."
以下代码是使用TransactionScope的此类方法的示例:
private void SomeMethod()
{
try
{
using (var _transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
using (SqlConnection _connection = new SqlConnection(connectionstring))
{
_connection.Open();
DoSomething()...
}
_transactionScope.Complete();
}
}
catch (TransactionAbortedException e)
{
nlog.Error(string.Format("The transaction has been aborted: {0}", e.Message));
throw e;
}
catch (Exception e)
{
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
似乎调用"_transactionScope.Complete()"不足以杀死事务范围.有没有人知道我做错了什么?
提前致谢!
更新 感谢您的回复.经过几次测试后,我发现只有在一个方法中有多个查询时才会出现此问题.例如:
try
{
using (TransactionScope _transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
using (SqlConnection _connection = new SqlConnection(connectionstring))
{
_connection.Open();
//new method:
using …Run Code Online (Sandbox Code Playgroud)