简体   繁体   English

何时在 DbContext 构造函数与 OnConfiguring 中提供 DbContextOptions?

[英]when to provide DbContextOptions in DbContext constructor vs OnConfiguring?

What is the difference between providing a DbContext constructor that accepts DbContextOptions<MyFileContext> versus including OnConfiguring method that wires-up database?提供接受DbContextOptions<MyFileContext>DbContext构造函数与包括连接数据库的OnConfiguring方法有什么区别?

Are the two are equivalent?两者是等价的吗?

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;

namespace MyApp.Models
{
    public class MyDbContext : DbContext
    {
        // OPTION 1
        public MyDbContext(DbContextOptions<MyFileContext> options) : base(options)
        {
        }

        public DbSet<MyTable> MyTables { get; set; }

        // OPTION 2
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);

            optionsBuilder.UseSqlServer(AppSettingsProvider.MySqlServerConnection);
        }

    }
}

Are the two are equivalent?两者是等价的吗?

It's complicated.情况很复杂。 (But ideally no); (但理想情况下没有);

The DbContextOptions can be supplied to the DbContext by overriding the OnConfiguring method or externally via a constructor argument.可以通过重写DbContextOptions方法通过构造函数参数在外部将OnConfiguring提供给DbContext

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

emphasis mine强调我的

Reference Configuring a DbContext参考配置 DbContext

暂无
暂无

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

相关问题 没有为此 DbContext 配置数据库提供程序,即使将 DbContextOptions 传递给构造函数 - No database provider has been configured for this DbContext, even when passing DbContextOptions to constructor 通过使用DbContextOptions的配置获取DbContext - Getting DbContext via Configuration with DbContextOptions 传递DbContextOptions <dbContext> 根据上下文 - Pass DbContextOptions<dbContext> to base context 在DbContext.OnConfiguring和AspCore Startup.ConfigureServices中都定义了optionsBuilder时,预期的结果是什么? - What are expected results when optionsBuilder is defined in both DbContext.OnConfiguring and AspCore Startup.ConfigureServices? 有什么方法可以检测何时从 Package 管理器控制台调用 DbContext.OnConfiguring()? - Any way to detect when DbContext.OnConfiguring() is being called from Package Manager Console? 可以通过覆盖DbContext.OnConfiguring来配置提供程序 - A provider can be configured by overriding the DbContext.OnConfiguring 如何在 dbContext OnConfiguring 生成中插入自定义代码? - How to insert custom codes in dbContext OnConfiguring generation? 从 DbContext 中提取 EF Core DbContextOptions - Extract EF Core DbContextOptions from DbContext 尝试使用 onconfiguring dbcontext 选项激活时无法解析服务类型 - Unable to resolve service for type while attempting to activate with onconfiguring dbcontext options 如果所有配置都在 DbContext 的 OnConfiguring 方法中,如何使用 AddDbContextPool - How to use AddDbContextPool if all configuration in OnConfiguring method of DbContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM