简体   繁体   English

通过使用DbContextOptions的配置获取DbContext

[英]Getting DbContext via Configuration with DbContextOptions

I am trying to get the DbContext I registered with options via services.AddDbContext(...) on the service provider of the project, but when calling configuration.Get<ModelContext> it can not be constructed as the options apparently weren't provided and therefore also no database provider is given. 我试图通过项目的服务提供者上的services.AddDbContext(...)获取通过选项注册的DbContext ,但是在调用configuration.Get<ModelContext>时无法构建,因为显然没有提供选项因此也没有给出数据库提供者。

I am using ASP.NET Core 2.2 with Entity Framework Core 2.2.3 and my DbContext is defined in a separate project. 我将ASP.NET Core 2.2与Entity Framework Core 2.2.3一起使用,并且DbContext是在单独的项目中定义的。

My DbContext : 我的DbContext

public class ModelContext : DbContext
{
    public ModelContext(DbContextOptions<ModelContext> options) : base(options) { }

    public ModelContext() { }
}    

I did not override OnConfiguring(DbContextOptionsBuilder) in ModelContext . 我没有重载OnConfiguring(DbContextOptionsBuilder)ModelContext

public class StartUp
{
    public void ConfigureServices(IServiceCollection services)
    {
        public services.AddEntityFrameworkSqlServer();
        services.AddDbContext<ModelContext>(options => options.UseSqlServer(modelConnectionString));
    }
}

In the controller (or anywhere really) I call public HomeController(IConfiguration configuration) => _modelContext = configuration.Get<ModelContext>(); 在控制器中(或实际上在任何地方),我称为public HomeController(IConfiguration configuration) => _modelContext = configuration.Get<ModelContext>(); which throws the unexpected exception. 引发意外的异常。

What I specifically get is an InvalidOperationException with the message: 我专门得到的是带有消息的InvalidOperationException

No database provider has been configured for this DbContext. 没有为此DbContext配置数据库提供程序。 A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. 可以通过重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。 If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext. 如果使用AddDbContext,则还应确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。

According to the documentation I read and examples I looked at, the ModelContext should be created with the options I defined when calling AddDbContext<ModelContext> . 据我看着我阅读文档和实例中, ModelContext应与打电话时我定义的选项来创建AddDbContext<ModelContext> Is the Get method the wrong one to use? Get方法使用错误吗?

After configuring the db context service in "ConfigureServices" method of the Startup.cs file with something like this : 在Startup.cs文件的“ ConfigureServices”方法中配置数据库上下文服务后,如下所示:

var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<BottinContext>(options => options.UseSqlServer(connectionString)) ;

Simply add a : 只需添加一个:

ModelContext db

parameter to the constructor of your controller and let DI magic happen. 参数传递给控制器​​的构造函数,并让DI魔术发生。

If you've got many controllers and wish to simplify things, you can use a base contructor that holds the db context 如果您有许多控制器并且希望简化操作,则可以使用一个包含数据库上下文的基本构造器。

  public BaseController(ModelContext context /* as well as other injections */)
    {
      _db = context;
    }
    internal ModelContext _db;

you are trying to get dbContxt instance in a wrong way. 您试图以错误的方式获取dbContxt实例。 Get method is not used to get instance of dbContext object that you registered with dependency injection container. Get方法不用于获取您在依赖项注入容器中注册的dbContext对象的实例。 if you want to get instance of your dbContext class that you registered you can inject it through construction injection for example 如果要获取注册的dbContext类的实例,则可以通过构造注入来注入它

public class RepositoryWrapper : IRepositoryWrapper
{
        private readonly ModelContext _modelContext;


        public RepositoryWrapper(ModelContext modelContext)
        {
        _modelContext= modelContext;
        }
}

is something i am doing in my project. 是我在项目中正在做的事情。

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

相关问题 传递DbContextOptions <dbContext> 根据上下文 - Pass DbContextOptions<dbContext> to base context 从 DbContext 中提取 EF Core DbContextOptions - Extract EF Core DbContextOptions from DbContext 何时在 DbContext 构造函数与 OnConfiguring 中提供 DbContextOptions? - when to provide DbContextOptions in DbContext constructor vs OnConfiguring? 在EF Core中创建DbContext实例时如何传递DbContextOptions值 - How to pass DbContextOptions value while creating DbContext instance in EF Core 在asp.core中我的dbcontext问题。 不知道如何使用DbContextOptions对象 - issue with my dbcontext in asp.core. Dont know how to use DbContextOptions object DbContext配置初始化问题 - DbContext Configuration initialization problem 没有为此 DbContext 配置数据库提供程序,即使将 DbContextOptions 传递给构造函数 - No database provider has been configured for this DbContext, even when passing DbContextOptions to constructor C# DbContext 问题:参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions” - C# Problem with DbContext: Argument 1: cannot convert 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions' EF7 DBContext的配置错误? - EF7 Incorrect configuration of the DBContext? 使用流畅的API配置模拟DbContext - Mocking DbContext with fluent API configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM