实体框架和SQL Server的保存点 [英] Entity Framework and SQL Server Savepoints
问题描述
我问了一个不同的问题有关嵌套事务,并回答我的问题,教育了我足够的帮我意识到我是问这个问题不好。所以这里有一个更好的问题。
I asked a different question about Nested Transactions and the answer to my question educated me enough to help me realize I was asking the question poorly. So here's a better question.
我如何能够有效地执行SQL Server的保存点(的链接1 ,链接2 )与在实体框架建立了一个DAL 4.0?
How can I effectively implement SQL Server Savepoints (link 1, link 2) with a DAL built upon Entity Framework 4.0?
我想写出下面code和有它的工作方式是SQL Server的保存点
I would LIKE to write the following code and have it work in a way that SQL Server's SAVEPOINTS
public void Bar()
{
using (var ts = new TransactionScope())
{
var ctx = new Context();
DoSomeStuff(ctx);
bool isSuccessful;
using (var spA = new SavePoint("A")) // <-- this object doesn't really exist, I don't think
{
isSuccessful = DoSomeOtherStuff(ctx);
if (isSuccessful)
spA.Complete(); // else rollback bo prior to the beginning of this using block
}
Log(ctx, isSuccessful);
ts.Complete();
}
}
有没有这样的方式做任何事情,甚至接近类似这种,或者说起到很好地与EF4别的东西吗? (我们使用自定义的自我跟踪POCO实体)
Is there such a way to do anything even close to resembling this, or something else that plays nicely with EF4? (we use custom self-tracking POCO entities)
推荐答案
这是不是一个相当完整的答案,但我怀疑这样的事情可能会下降正确的道路。我的问题是,<一个href="http://stackoverflow.com/questions/6989782/possible-to-get-sqltransaction-from-ambient-transaction">I'm不完全知道如何获得的SqlTransaction而在一个TransactionScope 。
This isn't quite a full answer, but I suspect something like this might be going down the right path. My problem is that I'm not entirely sure how to get a SqlTransaction while in a TransactionScope.
/// <summary>
/// Makes a code block transactional in a way that can be partially rolled-back. This class cannot be inherited.
/// </summary>
/// <remarks>
/// This class makes use of SQL Server's SAVEPOINT feature, and requires an existing transaction.
/// If using TransactionScope, utilize the DependentTransaction class to obtain the current Transaction that this class requires.
/// </remarks>
public sealed class TransactionSavePoint : IDisposable
{
public bool IsComplete { get; protected set; }
internal SqlTransaction Transaction { get; set; }
internal string SavePointName { get; set; }
private readonly List<ConnectionState> _validConnectionStates = new List<ConnectionState>
{
ConnectionState.Open
};
public TransactionSavePoint(SqlTransaction transaction, string savePointName)
{
IsComplete = false;
Transaction = transaction;
SavePointName = savePointName;
if (!_validConnectionStates.Contains(Transaction.Connection.State))
{
throw new ApplicationException("Invalid connection state: " + Transaction.Connection.State);
}
Transaction.Save(SavePointName);
}
/// <summary>
/// Indicates that all operations within the savepoint are completed successfully.
/// </summary>
public void Complete()
{
IsComplete = true;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
if (!IsComplete)
{
Transaction.Rollback(SavePointName);
}
}
}
这会被消耗掉如此,非常类似于一个TransactionScope:
This would be consumed as such, very similarly to a TransactionScope:
SqlTransaction myTransaction = Foo();
using (var tsp = new TransactionSavePoint(myTransaction , "SavePointName"))
{
try
{
DoStuff();
tsp.Complete
}
catch (Exception err)
{
LogError(err);
}
}
这篇关于实体框架和SQL Server的保存点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!