[英]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
:
CommandTimeOutInSeconds
as a parameter to the constructor. CommandTimeOutInSeconds
作为参数传递给构造函数。 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.