?
Solved

Changing the CommandTimeout property in the Entitiy Frame work

Posted on 2014-03-15
6
Medium Priority
?
1,357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 63

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 63

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
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 

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 63

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 63

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

771 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