Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Command Timeout in entity framework 4.3

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

like image 575
Saher Ahwal Avatar asked Jul 31 '12 19:07

Saher Ahwal


People also ask

How do I set the execution timeout in Entity Framework?

Set database timeout in Entity Framework Try this on your context:...public class MyDatabase : DbContext { public MyDatabase () : base(ContextHelper. CreateConnection("Connection string"), true) { ((IObjectContextAdapter)this). ObjectContext. CommandTimeout = 180; } } ...

How do I increase SQL timeout in Entity Framework?

You can use DbContext. Database. CommandTimeout = 180; It's pretty simple and no cast required.

What is the default command timeout entity framework?

Sometimes, however, you might also want to include a task that requires longer than the default command timeout value (30 seconds in SQL Server) such as importing a lot of data.


3 Answers

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

((IObjectContextAdapter)context).ObjectContext.CommandTimeout = 180; 
like image 146
bricelam Avatar answered Sep 24 '22 19:09

bricelam


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();
        }
like image 26
Maurice Jordan Avatar answered Sep 20 '22 19:09

Maurice Jordan


this command is enough.

((System.Data.Entity.Infrastructure.IObjectContextAdapter) context).ObjectContext.CommandTimeout
                = 180;
like image 21
m-Abrontan Avatar answered Sep 21 '22 19:09

m-Abrontan