gpt4 book ai didi

asp.net-core-2.1 - 即使我明确指定了 ASPNETCORE_ENVIRONENT=Development,ASP.NET Core 2.1 应用程序也没有选择 appsettings.Development.json。为什么?

转载 作者:行者123 更新时间:2023-12-03 20:29:29 25 4
gpt4 key购买 nike

所以我正在研究 ASP.NET Core 2.1 Web API。我通过以下两个函数为“开发”和“生产”明确指定了两组配置:

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(option => option
.UseMySql(Configuration.GetConnectionString("DefaultConnection"))
}

public void ConfigureDevelopmentServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(option => option
.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
}

在 appsettings.json 中,我有:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost; Database=datingApp; Uid=appuser; Pwd=zhanxucong"
}
}

在 appsettings.Development.json 中,我有:
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=DatingApp.db"
}
}
}

当我通过教程学习时,导师说按照惯例,当设置 ASPNETCORE_ENVIRONMENT=Development 时,ASP.NET Core 会调用更具体的配置函数(即 ConfigureDevelopmentServices)并使用更具体的 appsettings 配置文件(即 appsettings.Development. json)。我可以确认它在我的 Windows 和 Mac 上运行良好。但是,当我在 Linux 上运行此应用程序时,在使用上述设置运行 ef 迁移后,出现以下错误:
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.1.4-rtm-31024 initialized 'DataContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: None
System.ArgumentException: Keyword not supported: 'server'.
at Microsoft.Data.Sqlite.SqliteConnectionStringBuilder.GetIndex(String keyword)
at Microsoft.Data.Sqlite.SqliteConnectionStringBuilder.set_Item(String keyword, Object value)
at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteRelationalConnection.CreateReadOnlyConnection()
at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists()
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Keyword not supported: 'server'.

知道为什么会发生这个错误吗?它与Linux区分大小写有关吗?谢谢!

更新:我的程序类和我的启动类(由 dotnet-cli 工具通过运行“dotnet new webapi”提供的默认构造函数:

程序类:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}

启动类:
public class Startup
{
public IConfiguration Configuration { get; }

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

// ConfigureServices, ConfigureDevelopmentServices, Configure methods.
}

最佳答案

感谢您提供的所有答案。我真的很感激,我学到了很多。

至于我的bug,其实是因为一个非常愚蠢的错误:

在我的 appsettings.Development.json 文件中,"ConnectionStrings"嵌套在 "Logging" 内.因此,当ConfigureDevelopmentServices方法被调用,将找不到ConnectionStrings在 appsettings.Development.json 文件中,并将回退到 appsettings.json 中指定的配置,该配置用于连接到 MySQL 数据库。因此,我的问题陈述中显示了错误。

此观察的灵感来自 this GitHub issue .

关于asp.net-core-2.1 - 即使我明确指定了 ASPNETCORE_ENVIRONENT=Development,ASP.NET Core 2.1 应用程序也没有选择 appsettings.Development.json。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52638473/

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