Solved

Changing the CommandTimeout property in the Entitiy Frame work

Posted on 2014-03-15
6
1,218 Views
Last Modified: 2014-03-17
Hey guys,

New to EF. I need to increase the CommandTimeout of the context cobject. I can do it in the constructor of the generated code associated with the edmx file but that is auto-generated code which will be overwritten.

What's the best way to do that. I'm using .Net 4.5.

Thanks.
0
Comment
Question by:Aamer69
  • 4
  • 2
6 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39931745
Hi Aamer69;

What you can do is to create a partial class of the DbContext and create a new constructor which will take a single value of type integer. In this constructor you call the default constructor by using : this(). In this constructor you get a reference to the ObjectContext by casting the DbContext and with the ObjectContext you can adjust the CommandTimeout property. Please note that this partial class does not need to restate that it inherits from DbContext as the code generated from the model. When you create a DbContext just pass in the CommandTimeout you want or call it without a parameter and you will get the default CommandTimeout. Also this class needs to be in the same namespace as the generated code is in and you also need the using statement.

using System.Data.Entity.Infrastructure;

namespace WindowsFormsApplication11
{
    public partial class NorthwindEntities
    {
        public NorthwindEntities(int timeout)
            : this()
        {
            ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = timeout;
        }
    }
}

Open in new window

In the above the class name is NorthwindEntities which needs to be changed to the DbContext class that you have.
0
 

Author Comment

by:Aamer69
ID: 39932000
So,

Instead of calling the original constructor, I will be making a call to this constructor everywhere I am trying to get the instance of the context, right?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39932015
Yes if you want to modify the CommandTimeout for a particular DbContext you would use this new constructor which will call the default constructor that will use the connection string it has and then modifies the CommandTimeout.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Aamer69
ID: 39933044
There are a lot of places where the default constructor is called. Do I have to modify all those calls or is there another way?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39933111
If you want to change the CommandTimeout on all of them then YES.

You could create a class who's  only responsibility is for creating and destroying the connections and then you only have one place to make changes in.  Of course this means the first time you need to remove creating DbContext to calling a function to get an instance of a DbContext.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39935537
Hi Aamer69;

In order for me to improve my ability to provide a better solution can you please provide what would have made this solution worth a grade of "A".
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question