?
Solved

Changing the CommandTimeout property in the Entitiy Frame work

Posted on 2014-03-15
6
Medium Priority
?
1,475 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 64

Accepted Solution

by:
Fernando Soto earned 1500 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 64

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 64

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 64

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Over time, the online landscape has altered considerably, but that’s nothing compared to the up-and-coming trends that will shape the web design industry in the coming year. Keep reading to find out which trends will shape B2B web design in 2018.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

839 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