gpt4 book ai didi

c# - 从 appsettings.json 获取 ConnectionString,而不是在 .NET Core 2.0 应用程序中硬编码

转载 作者:行者123 更新时间:2023-12-02 00:02:24 24 4
gpt4 key购买 nike

我在 NET Core2.0 应用程序中有以下类(class)。

// required when local database does not exist or was deleted
public class ToDoContextFactory : IDesignTimeDbContextFactory<AppContext>
{
public AppContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<AppContext>();
builder.UseSqlServer("Server=localhost;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true");
return new AppContext(builder.Options);
}
}

当数据库不存在且必须在运行update-database时创建时,Core 2.0 中需要进行迁移。
Unable to create migrations after upgrading to ASP.NET Core 2.0

我不想在 2 个地方(此处和 appsettings.json 中)有 ConnectionString,而只在 .json 中所以我尝试更换

"Server=localhost;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true"

ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString

但它不起作用,我得到了空值。

更新1:
请注意,在 Core 2 中不需要显式添加 .json,因此问题不在于该文件。
https://andrewlock.net/exploring-program-and-startup-in-asp-net-core-2-preview1-2/

更新2:
另外,我已经使用配置将 ConnectionString 从 .json 发送到 Context:

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
}

但是我不能将其用于ToDoContextFactory,因为它没有配置,并且ToDoContextFactory由迁移使用,因此应用程序根本没有运行。

解决方案:根据@JRB 的回答,我让它像这样工作:

public AppContext CreateDbContext(string[] args)
{
string projectPath = AppDomain.CurrentDomain.BaseDirectory.Split(new String[] { @"bin\" }, StringSplitOptions.None)[0];
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(projectPath)
.AddJsonFile("appsettings.json")
.Build();
string connectionString = configuration.GetConnectionString("DefaultConnection");

var builder = new DbContextOptionsBuilder<AppContext>();
builder.UseSqlServer(connectionString);

return new AppContext(builder.Options);
}

最佳答案

第 1 步:在 OnConfiguring() 中包含以下内容

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
}

第 2 步:创建 appsettings.json:

  {
"ConnectionStrings": {
"DefaultConnection": "Server=YOURSERVERNAME; Database=YOURDATABASENAME; Trusted_Connection=True; MultipleActiveResultSets=true"
}
}

第 3 步:将 appsettings.json 硬拷贝到正确的目录

  Hard copy appsettings.json.config to the directory specified in the AppDomain.CurrentDomain.BaseDirectory directory. 
Use your debugger to find out which directory that is.

假设:您已经在项目中包含了 Microsoft.Extensions.Configuration.Json 包(从 Nuget 获取)。

关于c# - 从 appsettings.json 获取 ConnectionString,而不是在 .NET Core 2.0 应用程序中硬编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45796776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com