1

My application is an ASP.NET Core 1.0 Web API. I am using Entity framework.

I wan't to change the CommandTimeout like this:

public class MyDbContext: DbContext
{
   private const int Timeout = 180;

   public MyDbContext(DbContextOptions options): base(options)
   {
       this.Database.SetCommandTimeout(Timeout);
   }
}

this works if Timeout is defined in my class.

However, I would like to have the value of my Timeout inside my appsettings.json file like this:

  "DbContextSettings": {
    "Timeout": "180"
  }

so there are two ways to set the CommandTimeout now:

  1. Pass the CommandTimeOutInSeconds as a parameter to the constructor.
  2. Get the value out of appsettings.json inside the constructor of my class

I don't know how to achieve one of the ways.

Is this possible? or is there any known workaround?

Edit

Iam never really Initailizing MyDbContext. I do this in ConfigureServices in the Startup class like this:

services.AddDbContext<MyDbContext>(db => db.UseSqlServer("secret"));

Edit 2

@dennisanberlin so let's say I have a controller calling the following method:

public class SomeRepository
{

  private MyDbContext context;

  public SomeRepository(MyDbContext myContext)
  {
     this.context = myContext;
  }

  public Person SomeMethodCalledByController(){
     return myContext.SomeTableWhichContainsPersons.FirstOrDefault(person => person.name == "Bob");
  }
}

The class SomeRepository is never getting initialized by me. Iam doing this via dependency injection in the startup like this:

services.AddTransient<SomeRepository>();

The class knows about MyDbContext from the above shown line:

services.AddDbContext<MyDbContext>(db => db.UseSqlServer("secret"));

Therefore I never pass an object of MyDbContext directly to any of my classes working with the database.

0

1 Answer 1

2

You can pass the timeout to your constructor like this:

public MyDbContext(DbContextOptions options, int timeout): base(options)
   {
       Timeout = timeout;
       this.Database.SetCommandTimeout(Timeout);
   }

And here is an example on how to read settings from your App settings.json csharpcorner

EDIT

public MyDbContext(DbContextOptions options): base(options)
   {
       int timeout = //return timeout setting from appsettings.json
       this.Database.SetCommandTimeout(Timeout);
   }

You can try to do it that way, that you read your appsettings.json inside your constructor.

Sign up to request clarification or add additional context in comments.

10 Comments

But how do I pass the timeout to the constructor? Iam never initializing MyDbContext, see my edit
Maybe you can initiliaze it before you to your AddDb Method.
the constructor gets called at every request on my sql database so setting the value once might not work
Like ‘MyDbContext dbcontext = new MyDbContext(paramater);‘
As I said the constructor of the class gets called my the entityframework itself at every sql statement on the database. I can't call the constructor by my self. And even if I would, I would have to to this at every database call which would be like 3000 lines of code and iam not even sure if it would work then
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.