如何使用 ConfigurationManager 从 appsetting 获取连接字符串?

How can I get connectionstring from appsetting using ConfigurationManager?

我创建了一个 asp.net 核心网络 api 项目,其中包含 Microsoft.EntityFrameworkCore.SqlServer、Microsoft.EntityFrameworkCore.Tools 和 System.Configuration.ConfigurationManager。我 运行 脚手架命令 Scaffold-DbContext "Data Source=DEX-LEP3LOXH0M5\SQLEXPRESS;Initial Catalog=LibraryStore;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models.

在从脚手架 cmd 创建的 Context.cs 文件中创建了这个方法:

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                 optionsBuilder.UseSqlServer("Data Source=DEX-LEP3LOXH0M5\SQLEXPRESS;Initial Catalog=LibraryStore;Integrated Security=True");
               
            }
        }

当我 运行 到控制器的路由路径时,它工作得很好,我能够看到来自数据库的记录。但是在那条 #warning 消息上,它提供了一个 link 我遵循了它。在我的 appsettings.json 文件中,我通过以下方式添加了 ConnectionStrings

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "Default": "Data Source=DEX-LEP3LOXH0M5\SQLEXPRESS;Initial Catalog=LibraryStore;Integrated Security=True"
  }
}

然后我的下一步是在 startup.cs 中的 ConfigureServices 中添加代码:

public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "LibraryWebAPI", Version = "v1" });
            });
            services.AddDbContext<LibraryStoreContext>(options =>
                  options.UseSqlServer(Configuration.GetConnectionString("Default")));
        }

然后它说要更改创建的 Context.cs 文件中的 OnConfiguring()

我做了以下事情:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                
                optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
            }
        }

但随后它会在行 optionsBuilder:

上生成错误消息

System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.

如果相同的连接字符串在遵循 link to add connection to connectionstring in appsettings 之前有效,是否有返回 null 的原因?


包括我的控制器:

        [HttpGet]
        [Route("GetAllDetails")]
        public IEnumerable<LibraryInfo> GetDetails()
        {
            using (var context = new LibraryStoreContext())
            {
                // get all library details
                return context.LibraryDetails.ToList();
            }
        }

.Net Core 应用程序使用 appsettings.json 而不是 web.config 文件。

因为 .Net Core 应用程序是自托管的并且几乎可以 运行 在任何平台上,它们不再必须托管在 IIS 上。 .Net Core 应用程序设置默认以 Json 格式 (appsettings.json) 存储,而 .Net Framework 应用程序配置以 XML 格式存储在 web.config 文件中 这就是为什么在使用 ConfigurationManager

时它不应该与您的 appsettings.json 一起使用

根据您发送的 GUID,他们说要在 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

中进行更改

但是据说.net core应用上没有这个

我确实找到了一种更舒适的方式来维护生成的迁移,您不必 运行 来自 Web 项目的命令。

DbContext

internal class ContosoContext : DbContext
{
    private readonly IConfiguration configuration;
    public ContosoContext(IConfiguration configuration)
    {
        this.configuration = configuration;
    }
    public ContosoContext()
    {
        this.configuration = null;
    }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        if (configuration == null)
        {
            // Only used when generating migrations
            // Contoso.Data = name of the project where your migrations should reside
            var migrationsConnectionString = @"Server=(localdb)\mssqllocaldb;Database=Contoso;Trusted_Connection=True;ConnectRetryCount=0";
            optionsBuilder.UseSqlServer(migrationsConnectionString, options => options.MigrationsAssembly("Contoso.Data"));
        }
        else
        {
            optionsBuilder.UseSqlServer(configuration.GetConnectionString("Contoso"));
        }
    }
}

注册DbContext

services.AddDbContext<ContosoContext>(options => {
   options.UseSqlServer(Configuration.GetConnectionString("Contoso"));
});
  • 当运行连接应用程序(web项目)时,将调用带参数的构造函数。
  • Contoso.Data 文件夹 (dotnet ef migrations add myawesomemigration) 生成迁移时,将调用无参数构造函数。

这减少了在生成数据库迁移期间过多指定参数的需要:

dotnet ef migrations add AddIdentity