Solved

Changing the CommandTimeout property in the Entitiy Frame work

Posted on 2014-03-15
6
1,164 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Aamer69
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Read about why website design really matters in today's demanding market.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to count occurrences of each item in an array.

743 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

8 Experts available now in Live!

Get 1:1 Help Now