Set Command Timeout in entity framework 4.3
Asked Answered
I

3

33

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

Interlining answered 31/7, 2012 at 19:16 Comment(1)
For newer EF versions, see: https://mcmap.net/q/86651/-entity-framework-timeoutsAnatomist
B
64

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

((IObjectContextAdapter)context).ObjectContext.CommandTimeout = 180;
Bombay answered 31/7, 2012 at 20:13 Comment(2)
Edited my question : I confused command and connect timeout. SorryInterlining
will you please suggest me where we should we have to apply this code in our project ???Canova
T
11

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();
        }
Tice answered 6/1, 2015 at 15:2 Comment(2)
Perfect answer with sample code where to put. Thanks.Gadoid
Note that since this class is usually generated code, it would probably be better to make this change in the .tt file or some other initialization code. Otherwise the change will get clobbered next time you update the data model and regenerate.Maytime
L
9

this command is enough.

((System.Data.Entity.Infrastructure.IObjectContextAdapter) context).ObjectContext.CommandTimeout
                = 180;
Lanceolate answered 30/6, 2013 at 12:48 Comment(1)
will you please suggest me where we should we have to apply this code in our project ???Canova

© 2022 - 2024 — McMap. All rights reserved.