如何将Linq中的Arithabort设置为Entities? [英] How to set Arithabort on in Linq to Entities?

查看:78
本文介绍了如何将Linq中的Arithabort设置为Entities?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何在Linq中将Arithabort设置为SQL,但在Linq中为Entities却一无所获,我找到了大量答案.在Linq to SQL中,您可以执行以下操作:

I've found tons of answers for how to set Arithabort on in Linq to SQL, but nothing in Linq to Entities. In Linq to SQL, you can do this:

using (var conn = new SqlConnection(connectionString)){
  cmd = conn.CreateCommand();
  cmd.Connection.Open();
  cmd.CommandText = "set arithabort on;";
  cmd.ExecuteNonQuery(); // Line 5
  using (var db = new MyDataContext(conn)) {
    ...
  }
}

但是如果我做完全相同的事情,只是在上面的代码片段中用EntityConnection替换了SqlConnection,我在第5行会收到运行时错误:

but if I do exactly the same thing, just substituting EntityConnection from SqlConnection in the code snippet above, I get a runtime error on Line 5:

查询语法无效.标识符'arithabort',第1行,第5列附近.

The query syntax is not valid. Near identifier 'arithabort', line 1, column 5.

我猜想这与以下事实有关:Linq2Sql是为SQL Server硬接线的,而EF可以在其他DB上工作.那么诀窍是什么?

I'm guessing it's connected to the fact that Linq2Sql is hardwired for SQL Server, whereas EF can work on other DBs. So what's the trick?

推荐答案

SqlConnection并非特定于linq-to-sql或EF,您仍然可以将代码与EF一起使用.但是,您必须创建一个接收SqlConnection的EntityConnection.

The SqlConnection is not specific to linq-to-sql or EF, you can still use the code with EF. But you must create an EntityConnection that receives the SqlConnection.

EntityConnection.CreateCommand创建一个 EntityCommand ,它希望将Entity SQL作为命令文本,而不是原始SQL命令.

EntityConnection.CreateCommand creates an EntityCommand, which expects Entity SQL as command text, no raw SQL commands.

一种替代方法是使用上下文的ExecuteStoreQuery命令(ObjectContext)或context.Database.ExecuteSqlCommand(DbContext).

An alternative is to use the context's ExecuteStoreQuery command (ObjectContext), or context.Database.ExecuteSqlCommand (DbContext).

这篇关于如何将Linq中的Arithabort设置为Entities?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆