将参数传递给继承自DbContext的类 [英] Pass parameter to class which inherits from DbContext

查看:976
本文介绍了将参数传递给继承自DbContext的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个 ASP.NET Core 1.0 Web API 。我使用实体框架



我不会更改 CommandTimeout 这样:

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

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

如果超时在我的课程中定义。



但是,我想要具有我的超时值我的 appsettings.json 文件如下:

 DbContextSettings :{
超时:180
}

现在有两种设置 CommandTimeout 的方法:


  1. 通过 CommandTimeOutInSeconds 作为构造函数的参数。

  2. appsettings.json 中获取值我的课程的构造函数

我不知道如何实现其中一种方式。



这可能吗?还是有任何已知的解决方法?



修改



Iam从不真正地初始化 MyDbContext 。我在 ConfigureServices 启动类中这样做:

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

编辑2



  public class SomeRepository 
{

私人MyDbContext上下文;

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

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

SomeRepository 从来没有被我初始化。 Iam通过依赖注入启动中执行此操作,如下所示:

  services.AddTransient< SomeRepository>(); 

该类从上面了解 MyDbContext 显示行:

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

所以我从来没有传递一个 MyDbContext 直接到我使用数据库的任何类。

解决方案

你可以将这个超时传递给你的构造函数: >

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

这里是一个如何从App settings.json读取设置的示例 csharpcorner



编辑

  public MyDbContext(DbContextOptions options):base(options)
{
int timeout = //从appsettings.json返回超时设置
this.Database.SetCommandTimeout(Timeout);
}

你可以尝试这样做,你读你的appsettings.json您的构造函数内。


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.

解决方案

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.

这篇关于将参数传递给继承自DbContext的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆