C# 实体框架 - 当前命令超时值是多少

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17049055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:28:17  来源:igfitidea点击:

Entity Framework - what is the current command timeout value

c#entity-frameworkentity-framework-5

提问by Oren

I'm using Entity Framework 5 and I wish to know the command timeout value.

我正在使用 Entity Framework 5,我想知道命令超时值。

In order to do so, I cast the dbContextobject to an ObjectContextand I access the CommandTimeoutproperty.

为此,我将dbContext对象强制转换为 anObjectContext并访问该CommandTimeout属性。

int ? currentCommandTimeout = ((IObjectContextAdapter)dbContext).ObjectContext.CommandTimeout;

The current value of this property is null which means that the current command timeout is the default value of the underlying provider.

此属性的当前值为 null,这意味着当前命令超时是基础提供程序的默认值。

  1. Who is the underlying provider?
  2. How can I read (via EF code) the current command timeout value in this case?
  1. 谁是底层提供者?
  2. 在这种情况下,如何读取(通过 EF 代码)当前命令超时值?

MSDN ObjectContext CommandTimeout Property reference

MSDN ObjectContext CommandTimeout 属性参考

EDIT: Thank you for explaining how to set the command timeout and finding the default command timeout value in the documentation. However, the question remains open. How, if possible, can you read the command timeout value in case of default, via EF.

编辑:感谢您解释如何设置命令超时并在文档中找到默认的命令超时值。然而,这个问题仍然悬而未决。如果可能,您如何通过 EF 在默认情况下读取命令超时值。

回答by Bhushan Firake

From MSDN,

从 MSDN,

  • CommandTimeout property gets or sets the timeout value, in seconds, for all object context operations.
  • A nullvalue indicates that the default value of the underlying provider will be used.
  • CommandTimeout 属性获取或设置所有对象上下文操作的超时值(以秒为单位)。
  • 一个null值表示将使用基础提供程序的默认值。

So, if you are not setting it explicitly through code or passing it in your connection string (in MySQL) , then it is the default value of your provider.

因此,如果您没有通过代码明确设置它或在您的连接字符串(在 MySQL 中)中传递它,那么它就是您的提供者的默认值。

If you want to see a non-null value for CommandTimeout, then you will need to pass it in connectionString or set it through code.

如果要查看 的非空值CommandTimeout,则需要将其传递到 connectionString 或通过代码设置它。

Who is the underlying provider?

谁是底层提供者?

Underlying provider is the one you are passing in your connectionstring as providerName

底层提供程序是您在连接字符串中作为 providerName

<connectionStrings>
  <clear />
  <add name="Name" 
   providerName="System.Data.ProviderName" 
   connectionString="Valid Connection String;" />
</connectionStrings>

Here, System.Data.ProviderNameis your underlying provider.

在这里,System.Data.ProviderName是您的基础提供商。

If you are using MySql or MS Sql, According to the MySql Documentationand MSDN,

如果您使用的是 MySql 或 MS Sql,根据MySql 文档MSDN

  • The default value is 30 secs.
  • A value of 0 indicates an indefinite wait and should be avoided.

    Note :

  • 默认值为30 secs
  • 值 0 表示无限期等待,应避免。

    笔记 :

The default command timeout can be changed using the connectionstring attribute Default Command Timeoutin case of MySQL database providers.

对于Default Command TimeoutMySQL 数据库提供程序,可以使用 connectionstring 属性更改默认命令超时。

回答by Jason Massey

If you're using SQLServer then the default command timeout is 30 seconds.

如果您使用 SQLServer,则默认命令超时为 30 秒。

SqlCommand.CommandTimeout Property

SqlCommand.CommandTimeout 属性

回答by afr0

This should work

这应该工作

var ctx = new DbContext();
ctx.Database.CommandTimeout = 120;

回答by enoshixi

This is how I do it.

我就是这样做的。

ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext
int commandTimeout = objectContext.CommandTimeout 
    ?? objectContext.Connection.CreateCommand().CommandTimeout;

回答by RBT

  1. Who is the underlying provider?
  1. 谁是底层提供者?

For connecting to any database remotely and firing SQL queries you need a mechanism or a mediator (you can say) which understands the communication semantics of various actions to be taken while firing a query e.g. creating command, making connection, taking care of connection timeout, retry etc.

要远程连接到任何数据库并触发 SQL 查询,您需要一种机制或中介(可以说),它了解触发查询时要采取的各种操作的通信语义,例如创建命令、建立连接、处理连接超时,重试等

All these responsibilities are taken care of by providers for the database you are trying to connect to. These providers will have different implementations and classes w.r.t. the environment (C#, Java, Python,Perl etc) you are connecting from. So to connect to a MySQL database you will have different providers for java, .net, or python programming world.

所有这些职责都由您尝试连接的数据库的提供程序负责。这些提供程序将有不同的实现和类 wrt 您正在连接的环境(C#、Java、Python、Perl 等)。因此,要连接到 MySQL 数据库,您将拥有用于 java、.net 或 python 编程世界的不同提供程序。

  1. How can I read (via EF code) the current command timeout value in this case?
  1. 在这种情况下,如何读取(通过 EF 代码)当前命令超时值?

Nope. This is not possible. If you closely look at the dbContext.Database.CommandTimeoutproperty, it is of type int?. So the logistics of keeping this property as nullable int is only to represent the default value (as null). Entity Framework (EF) doesn't expose anything until you have set an explicit value in C# code. I verified this against following databases

不。这不可能。如果您仔细查看该dbContext.Database.CommandTimeout属性,它的类型为int?。因此,将此属性保持为可空 int 的逻辑只是表示默认值(作为 null)。在 C# 代码中设置显式值之前,实体框架 (EF) 不会公开任何内容。我根据以下数据库对此进行了验证

  • MySQL
  • SQL Server
  • MySQL
  • 数据库服务器

In both the cases I see that after creating the DB context object the command time out property is set to nullwhich suggests that it is using the default provided by the underlying provider.

在这两种情况下,我看到在创建 DB 上下文对象后,命令超时属性被设置为null表明它正在使用底层提供程序提供的默认值。

But yes, if you have set it to a non-null value once in your C# code through dbContext.Database.CommandTimeoutproperty, then you can of course read it again as it is a get-set property on the command object. It will always show you the new value which was set explicitly in code.

但是是的,如果您在 C# 代码中通过dbContext.Database.CommandTimeout属性将其设置为非空值,那么您当然可以再次读取它,因为它是命令对象上的 get-set 属性。它将始终向您显示在代码中明确设置的新值。

If you set it back to nullthen EF will again start using the default timeout of the underlying provider.

如果您将其重新设置为nullEF ,则 EF 将再次开始使用基础提供程序的默认超时。

Using Provider Classes in place of EF layer: If you don't use the ORM layer of entity framework and use the core classes of the provider instead then you can certainly see the default timeout value on initialization itself. Like when I ran below mentioned code for a MySQLdatabase with default command time out value already set in the connection string itself then you get to see 40.

使用提供程序类代替 EF 层:如果您不使用实体框架的 ORM 层而是使用提供程序的核心类,那么您当然可以看到初始化本身的默认超时值。就像当我运行下面提到的MySQL数据库代码时,默认命令超时值已经在连接字符串本身中设置,然后你会看到 40。

private static void TestingCommandTimeOutInMySql()
        {
            string connetionString = "Server=localhost;Database=sakila;Uid=root;Pwd=Passw0rd;default command timeout=40;";
            MySqlConnection con = new MySqlConnection(connetionString);
            try
            {
                cnn.Open();
                Console.WriteLine("Connection Open ! ");
                MySqlCommand cmd = new MySqlCommand("select * from actor", con);
                var timeoutValue = cmd.CommandTimeout; //shows 40
                con.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Can not open connection ! ");
            }
        }

If you don't set anything in the connection string it shows default value of 30 for MySQL. Here the CommandTimeoutis int, NOT int?. So EF certainly does apply some intelligence while exposing this property of provider classes which are ultimately exposed by EF through dbContext.Database.CommandTimeout. EF is just a wrapper over these provider classes with some additional intelligence.

如果您没有在连接字符串中设置任何内容,它会显示 MySQL 的默认值 30。这里CommandTimeoutint,不是int?。因此,EF 在公开提供程序类的此属性时确实应用了一些智能,这些提供程序类最终由 EF 通过dbContext.Database.CommandTimeout. EF 只是这些提供程序类的包装器,具有一些额外的智能。

Few additional details regarding connection stings :

关于连接刺的一些额外细节:

For SQL Server Provider:Microsoft SQL server connection string formats do not provide any property with the help of which you can set a custom command execution timeout. So setting a value during the initialization of the SQL Server provider is simply not possible. You can always change the value in C# code though after the intantiation/initialization. You can see more details here.

对于 SQL Server 提供程序:Microsoft SQL Server 连接字符串格式不提供任何属性,您可以借助这些属性设置自定义命令执行超时。因此,在 SQL Server 提供程序的初始化过程中设置一个值是不可能的。尽管在初始化/初始化之后,您始终可以更改 C# 代码中的值。您可以在此处查看更多详细信息。

For MySQL Provider:My SQL connection string formats have a support for setting the default command timeout explicitly. You can find the details here. You can mention a custom default command timeout value in the MySQL connection string like this -

对于 MySQL Provider:我的 SQL 连接字符串格式支持显式设置默认命令超时。您可以在此处找到详细信息。您可以在 MySQL 连接字符串中提及自定义默认命令超时值,如下所示 -

default command timeout=200;