gpt4 book ai didi

entity-framework-4 - 如何在 Entity Framework 中配置事务?

转载 作者:行者123 更新时间:2023-12-05 00:38:35 25 4
gpt4 key购买 nike

如何在 Entity Framework 4 中配置事务?在普通的旧 Ado.Net 中,我们有一个名为 SqlTransaction 的类,我们还可以为该事务指定隔离级别,如 Read_Committed、Read_UnCommitted 等。我在 Entity Framework 中找不到所有这些选项。我们如何配置它们?

最佳答案

您可以使用 TransactionScope类,并使用 TransactionOptions 设置隔离级别如所述 here :

Some of the overloaded constructors of TransactionScope accept a structure of type TransactionOptions to specify an isolation level, in addition to a timeout value. By default, the transaction executes with isolation level set to Serializable. Selecting an isolation level other than Serializable is commonly used for read-intensive systems. This requires a solid understanding of transaction processing theory and the semantics of the transaction itself, the concurrency issues involved, and the consequences for system consistency.



例如:
using (var context = new EFTestEntities())
{
context.AddToProducts(new Product { Name = "Widget" });
context.AddToProducts(new Product { Name = "Chotchky" });

TransactionOptions options = new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted, Timeout = TransactionManager.DefaultTimeout };

using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, options))
{
// do any EF work that you want to be performed in the transaction
context.SaveChanges();

// commit the transaction
scope.Complete();
}
}

关于entity-framework-4 - 如何在 Entity Framework 中配置事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5442194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com