[英]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 theDbContext
by overriding theOnConfiguring
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 inOnConfiguring
. 此属性可用于确定是否已设置选项,并跳过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类的配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.