简体   繁体   English

在DbContext.OnConfiguring和AspCore Startup.ConfigureServices中都定义了optionsBuilder时,预期的结果是什么?

[英]What are expected results when optionsBuilder is defined in both DbContext.OnConfiguring and AspCore Startup.ConfigureServices?

My ASP.NET core has this class which gets called first 我的ASP.NET核心具有此类,该类首先被调用

public class Startup
{ 
    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
         services.AddDbContext<IssuerContext>(options => 
             options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddMvc();
    }

And my context has this: 我的情况是这样的:

public class IssuerContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connString = "Server=(localdb)\\mssqllocaldb;Database=HavenServer;ConnectRetryCount=0;Trusted_Connection=True;MultipleActiveResultSets=true\"";
        optionsBuilder
            .UseLoggerFactory(MyConsoleLoggerFactory)
            .EnableSensitiveDataLogging(false)
            .UseSqlServer(connString, options => options.MaxBatchSize(150));

        base.OnConfiguring(optionsBuilder);
    }

What is the expected SQLServer options configuration when seemingly overlapping options are defined in two locations? 当在两个位置中定义了看似重叠的选项时,预期的SQLServer选项配置是什么?

It is explained in the Configuring a DbContext section of the documentation: 在文档的“ 配置DbContext”部分中对此进行了解释:

The DbContextOptions can be supplied to the DbContext by overriding the OnConfiguring method or externally via a constructor argument. DbContextOptions可以被供应到DbContext通过重写OnConfiguring方法或通过构造器参数外部。

If both are used, OnConfiguring is applied last and can overwrite options supplied to the constructor argument. 如果两者都使用,则OnConfiguring最后应用,并且可以覆盖提供给构造函数参数的选项。

In general, inside your OnConfiguring override you are supposed to check DbContextOptionsBuilder.IsConfigured property: 通常,应该在OnConfiguring覆盖内部检查DbContextOptionsBuilder.IsConfigured属性:

Gets a value indicating whether any options have been configured. 获取一个值,该值指示是否已配置任何选项。

This can be useful when you have overridden OnConfiguring to configure the context, but in some cases you also externally provide options via the context constructor. 当您重写OnConfiguring来配置上下文时,这很有用,但是在某些情况下,您还可以通过上下文构造函数在外部提供选项。 This property can be used to determine if the options have already been set, and skip some or all of the logic in OnConfiguring . 此属性可用于确定是否已设置选项,并跳过OnConfiguring部分或全部逻辑。

Eg 例如

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        var connString = "Server=(localdb)\\mssqllocaldb;Database=HavenServer;ConnectRetryCount=0;Trusted_Connection=True;MultipleActiveResultSets=true\"";
        optionsBuilder
            .UseLoggerFactory(MyConsoleLoggerFactory)
            .EnableSensitiveDataLogging(false)
            .UseSqlServer(connString, options => options.MaxBatchSize(150));
    }
    base.OnConfiguring(optionsBuilder);
}

Generally, both options will be applied with the configuration from the "OnConfiguring" method being " applied in addition to configuration " from the "ConfigureServices" method. 通常,这两个选项都将与“ OnConfiguring”方法中的配置和“ ConfigureServices”方法中的“配置”一起应用。 ConfigureServices is used to setup dependency injection for your DbContext so it will use those options as a constructor parameter. ConfigureServices用于为DbContext设置依赖项注入,因此它将这些选项用作构造函数参数。 Any additional configuration done in the OnConfiguring method will be appended or override configuration from the StartUp class. 在OnConfiguring方法中完成的所有其他配置都将附加或覆盖StartUp类中的配置。 However, in the example you have provided, you do not have a constructor in your DbContext, so configuration from the Startup class will not be used. 但是,在您提供的示例中,DbContext中没有构造函数,因此不会使用Startup类的配置。

Docs 文件

暂无
暂无

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

相关问题 可以通过覆盖DbContext.OnConfiguring来配置提供程序 - A provider can be configured by overriding the DbContext.OnConfiguring 有什么方法可以检测何时从 Package 管理器控制台调用 DbContext.OnConfiguring()? - Any way to detect when DbContext.OnConfiguring() is being called from Package Manager Console? 在 Startup.ConfigureServices 方法期间访问依赖注入对象 - Access dependency injection objects during Startup.ConfigureServices method 没有为此 DbContext 配置数据库提供程序。 可以通过覆盖 DbContext.OnConfiguring 来配置提供程序 - No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring 测试通过ASP.NET Core API通过Startup.ConfigureServices()配置的Polly重试polly - Test Polly retry polly configured via Startup.ConfigureServices() with ASP.NET Core API 何时在 DbContext 构造函数与 OnConfiguring 中提供 DbContextOptions? - when to provide DbContextOptions in DbContext constructor vs OnConfiguring? 如何获取我在Asp.Net核心的Startup.ConfigureServices中添加的所有授权策略? - How to get all Authorize Policies that I added in the Startup.ConfigureServices in Asp.Net core? 创建 Controller 时 DBContext 未解析(.NET5、ASPCore、EF) - DBContext Not Resolving When Controller Created (.NET5, ASPCore, EF) 如何在startup.cs的ConfigureServices方法中正确注入DbContext实例(ASP.net core 1.1)? - How to inject DbContext instance in the ConfigureServices method of startup.cs correctly (ASP.net core 1.1)? 启动时的KeyNotFoundException配置服务AddMvc() - KeyNotFoundException in Startup ConfigureServices AddMvc()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM