locked
EF 4.1 DbContext CommandTimeout? RRS feed

  • Question

  • How do you set the CommandTimeout for a EF4.1 Code First DbContext?

    Preemptively- Yes, this operation takes longer than 30 seconds to run.
    No, it's not an index issue.
    Yes, I _NEED_ all of the data at once.

    Wednesday, April 13, 2011 10:19 PM

Answers

  • Hello Storm, the command timeout needs to be set at the ObjectContext level. You can set it at least in the EF 4.1:

     

        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
    
        public class YourContext : DbContext
        {
          public YourContext()
            : base("YourConnectionString")
          {
            // Get the ObjectContext related to this DbContext
            var objectContext = (this as IObjectContextAdapter).ObjectContext;
    
            // Sets the command timeout for all the commands
            objectContext.CommandTimeout = 120;
          }
        }
    
    

    Hope this help you.

    Regards,

    Miguel.

     

    • Proposed as answer by Pawel Kadluczka Wednesday, April 20, 2011 5:18 AM
    • Marked as answer by Jackie-Sun Monday, April 25, 2011 5:41 AM
    Friday, April 15, 2011 7:44 PM

All replies

  • Hello Storm, the command timeout needs to be set at the ObjectContext level. You can set it at least in the EF 4.1:

     

        using System.Data.Entity;
        using System.Data.Entity.Infrastructure;
    
        public class YourContext : DbContext
        {
          public YourContext()
            : base("YourConnectionString")
          {
            // Get the ObjectContext related to this DbContext
            var objectContext = (this as IObjectContextAdapter).ObjectContext;
    
            // Sets the command timeout for all the commands
            objectContext.CommandTimeout = 120;
          }
        }
    
    

    Hope this help you.

    Regards,

    Miguel.

     

    • Proposed as answer by Pawel Kadluczka Wednesday, April 20, 2011 5:18 AM
    • Marked as answer by Jackie-Sun Monday, April 25, 2011 5:41 AM
    Friday, April 15, 2011 7:44 PM
  • That's what I was looking for, thanks.
    Friday, April 29, 2011 6:00 AM