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

前端 未结 2 812
独厮守ぢ
独厮守ぢ 2020-12-19 14:09

My ASP.NET core has this class which gets called first

public class Startup
{ 
    public IConfiguration Configuration { get; }

    // This method gets cal         


        
相关标签:
2条回答
  • 2020-12-19 14:51

    Generally, both options will be applied with the configuration from the "OnConfiguring" method being "applied in addition to configuration" from the "ConfigureServices" method. ConfigureServices is used to setup dependency injection for your DbContext so it will use those options as a constructor parameter. Any additional configuration done in the OnConfiguring method will be appended or override configuration from the StartUp class. 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.

    Docs

    0 讨论(0)
  • 2020-12-19 15:14

    It is explained in the Configuring a DbContext section of the documentation:

    The DbContextOptions can be supplied to the DbContext by overriding the OnConfiguring method or externally via a constructor argument.

    If both are used, OnConfiguring is applied last and can overwrite options supplied to the constructor argument.

    In general, inside your OnConfiguring override you are supposed to check DbContextOptionsBuilder.IsConfigured property:

    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. This property can be used to determine if the options have already been set, and skip some or all of the logic in OnConfiguring.

    E.g.

    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);
    }
    
    0 讨论(0)
提交回复
热议问题