具有 IsolationLevel Snapshot 的事务无法升级

Chr*_*lly 5 c# entity-framework transactions

我正在尝试通过 Entity v.4.0.30319 对存储过程的调用包装 TransactionScope。我不断遇到以下异常:

无法提升具有 IsolationLevel Snapshot 的事务。

我该如何解决这个问题?

底层存储过程基本上是一个表中的大插入语句。

我的代码如下:

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, GetTransactionOptions()))
{
    int? status;
    status = GetStatusIDFromEnum(newMatterCredential);

    using (MatterCredentialsEntities db = new MatterCredentialsEntities())
    {
        DateTime? objDateAnnounced = GenerateNullForDateTime(newMatterCredential.DateAnnounced);
        DateTime? objDateClosed = GenerateNullForDateTime(newMatterCredential.DateClosed);
        DateTime? objDateFinancialClosed = GenerateNullForDateTime(newMatterCredential.DateFinancialClosed);
        db.prcCreateCredential(Common.GetUserProfID(), newMatterCredential.MatterID, status, newMatterCredential.DescriptionSummary, newMatterCredential.DescriptionDetailed, newMatterCredential.BusinessEntitySectorID, newMatterCredential.BusinessEntityRoleID, newMatterCredential.UNGeographyID, newMatterCredential.ProjectName, newMatterCredential.ClientIndustryId, newMatterCredential.TransactionValue, newMatterCredential.TransactionCurrencyID, newMatterCredential.OtherParties, newMatterCredential.LegalAdvisers, newMatterCredential.DateAnnounced, newMatterCredential.DateClosed, newMatterCredential.DateFinancialClosed, newMatterCredential.Award, newMatterCredential.NotifyPartner, newMatterCredential.Notes);
    }

    scope.Complete();
}

public static TransactionOptions GetTransactionOptions()
{
    TransactionOptions tranOpt = new TransactionOptions();
    tranOpt.IsolationLevel = IsolationLevel.Snapshot;
    return tranOpt;
}
Run Code Online (Sandbox Code Playgroud)

jcr*_*r74 4

MSDN 表示您无法通过快照隔离来促进事务。

MSDN - 隔离级别枚举

快照 - 可以读取易失性数据。在事务修改数据之前,它会验证另一个事务在最初读取数据后是否更改了该数据。如果数据已更新,则会引发错误。这允许事务获取先前提交的数据值。当您尝试提升使用此隔离级别创建的事务时,将引发 InvalidOperationException 并显示错误消息:

具有 IsolationLevel Snapshot 的事务无法升级

自您启动事务以来,如果这是它正在参与的较大事务的一部分,则必须有其他东西正在更改数据

我建议将事务更改为可序列化。

public static TransactionOptions GetTransactionOptions()
{
    TransactionOptions tranOpt = new TransactionOptions();
    tranOpt.IsolationLevel = IsolationLevel.Serializable;
    return tranOpt;
}
Run Code Online (Sandbox Code Playgroud)

编辑:请参阅下文,确保您正在运行 MSDTC,因为这要创建分布式事务。