Solved

Changing the CommandTimeout property in the Entitiy Frame work

Posted on 2014-03-15
6
1,199 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
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
text on slider doesn't resize nicely 5 38
Need to pass a connection through class/Method 1 39
Animated .jpg? 13 59
What .NET URL re-routing tool did I use? 2 36
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now