Set Command Timeout in entity framework 4.3

33,988

Solution 1

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

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

Solution 2

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();
        }

Solution 3

this command is enough.

((System.Data.Entity.Infrastructure.IObjectContextAdapter) context).ObjectContext.CommandTimeout
                = 180;
Share:
33,988
Saher Ahwal
Author by

Saher Ahwal

Software Engineer at Microsoft LinkedIn Profile MEng Thesis: Optimizations to a massively parallel database and support of a shared scan architecture

Updated on June 26, 2020

Comments

  • Saher Ahwal
    Saher Ahwal almost 4 years

    I cannot find the a way to set the command timeout of a linq query using entity framework 4.3 and its' DbContext. How do I increase Commandtimeout in entity framework?

    EDIT I am actually looking for Command Timeout increase. I confused the two, it is the sql command that is timing out not the connection.

    Thanks