Set Command Timeout in entity framework 4.3

别等时光非礼了梦想. 提交于 2019-11-27 21:03:51
bricelam

If you're using DbContext, you'll first need to drop down to ObjectContext:

((IObjectContextAdapter)context).ObjectContext.CommandTimeout = 180;

I added the command timeout value in my Context class in an attempt to handle longer processing times for some of the stored procedures that are populating my application. Seems to have done the trick.

public partial class ExampleEntities : DbContext
    {
        public ExampleEntities()
            : base("name=ExampleEntities")
        {
            ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 180;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

this command is enough.

((System.Data.Entity.Infrastructure.IObjectContextAdapter) context).ObjectContext.CommandTimeout
                = 180;

I had problem with setting CommandTimeout when I use await, like this:

await _dbContext.Database.Connection.QueryAsync("Update ...."),

then I found in documentation https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.commandtimeout?view=netframework-4.8 that:

The CommandTimeout property will be ignored during asynchronous method calls such as BeginExecuteReader.

I changed to:

_dataWarehouseDbContext.Database.Connection.Query("Update ...")

and CommandTimeout start working :)

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