我正在使用SQL Server和ASP.NET.我有以下功能:
Using js = daoFactory.CreateJoinScope()
Using tran = New Transactions.TransactionScope()
'...
tran.Complete()
End Using
End Using
Run Code Online (Sandbox Code Playgroud)
但是,例外情况' 事务管理器已禁用其对远程/网络事务的支持.'被抛出.
JoinScope的描述:
Public Class JoinScope
Implements IJoinScope
Implements IDisposable
'...
End Class
Run Code Online (Sandbox Code Playgroud)
我已经在另一个具有相同环境的应用程序中以这种方式工作而没有问题,但在这里我遇到了这个问题.我该怎么做才能解决这个问题?
去年,Scott Guthrie 表示 "如果你想要对执行的SQL进行绝对控制,你实际上可以覆盖LINQ to SQL使用的原始SQL",但我找不到描述可扩展性方法的文档.
我想修改以下LINQ to SQL查询:
using (NorthwindContext northwind = new NorthwindContext ()) {
var q = from row in northwind.Customers
let orderCount = row.Orders.Count ()
select new {
row.ContactName,
orderCount
};
}
这导致以下TSQL:
SELECT [t0].[ContactName], (
SELECT COUNT(*)
FROM [dbo].[Orders] AS [t1]
WHERE [t1].[CustomerID] = [t0].[CustomerID]
) AS [orderCount]
FROM [dbo].[Customers] AS [t0]
至:
using (NorthwindContext northwind = new NorthwindContext ()) {
var q = from row in northwind.Customers.With (
TableHint.NoLock, TableHint.Index (0))
let orderCount = … 为了处理SQL超时我正在尝试使用SqlAzureExecutionStrategy(https://msdn.microsoft.com/en-us/data/dn456835.aspx)
我遇到的问题是,它可以防止"用户发起的交易",这似乎是实施"与(NOLOCK)",在EF(推荐方式http://www.hanselman.com/blog/GettingLINQToSQLAndLINQToEntitiesToUseNOLOCK.aspx,NOLOCK与Linq to SQL).
示例代码
public AspnetUser GetAspnetUserByUserName(string userName)
{
using (var tx = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadUncommitted }))
{
return context.AspnetUsers.Where(x => x.UserName == userName).FirstOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
抛出错误
配置的执行策略"SqlAzureExecutionStrategy"不支持用户启动的事务.有关其他信息,请参阅http://go.microsoft.com/fwlink/?LinkId=309381.
我已经看到了在每次调用的基础上关闭SqlAzureExecutionStrategy的答案,但是如果我的所有读取都忽略了策略,那将无法使用它.可以同时拥有"NoLock"和SqlAzureExecutionStrategy
linq entity-framework azure entity-framework-6 azure-sql-database
据我所知,使用a的"正确"方法TransactionScope是transactionScope.Complete();在退出using块之前始终调用.像这样:
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted }))
{
//...
//I'm using this as a NOLOCK-alternative in Linq2sql.
transactionScope.Complete();
}
Run Code Online (Sandbox Code Playgroud)
但是,我已经看到代码在没有它的情况下工作,甚至我学会了使用它的答案都 省略了它.所以我的问题是,它是否必须使用?