简体   繁体   English

将参数传递给从DbContext继承的类

[英]Pass parameter to class which inherits from DbContext

My application is an ASP.NET Core 1.0 Web API . 我的应用程序是ASP.NET Core 1.0 Web API I am using Entity framework . 我正在使用Entity framework

I wan't to change the CommandTimeout like this: 我不会像这样更改CommandTimeout

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. 如果在我的班级中定义了Timeout则此方法有效。

However, I would like to have the value of my Timeout inside my appsettings.json file like this: 但是,我想在我的appsettings.json文件中包含Timeout的值,如下所示:

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

so there are two ways to set the CommandTimeout now: 因此,有两种方法可以立即设置CommandTimeout

  1. Pass the CommandTimeOutInSeconds as a parameter to the constructor. CommandTimeOutInSeconds作为参数传递给构造函数。
  2. Get the value out of appsettings.json inside the constructor of my class 从类的构造函数中的appsettings.json获取值

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 . 我从来没有真正Initailizing MyDbContext I do this in ConfigureServices in the Startup class like this: 我在Startup类的ConfigureServices中执行以下操作:

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

Edit 2 编辑2

@dennisanberlin so let's say I have a controller calling the following method: @dennisanberlin,所以让我们说一个控制器调用以下方法:

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. 我从未初始化过SomeRepository类。 Iam doing this via dependency injection in the startup like this: 我在startup通过dependency injection来做到这一点,如下所示:

services.AddTransient<SomeRepository>();

The class knows about MyDbContext from the above shown line: 该类从上面显示的行知道MyDbContext

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. 因此,我永远不会将MyDbContext的对象直接传递MyDbContext数据库一起使用的任何类。

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 这里是如何读取你的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. 您可以尝试以这种方式进行操作,即在构造函数中读取appsettings.json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM