How to set Arithabort on in Linq to Entities?

ⅰ亾dé卋堺 提交于 2019-12-01 10:52:25

问题


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)) {
    ...
  }
}

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:

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

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?


回答1:


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 creates an EntityCommand, which expects Entity SQL as command text, no raw SQL commands.

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



来源:https://stackoverflow.com/questions/16438007/how-to-set-arithabort-on-in-linq-to-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!