即使我明确指定ASPNETCORE_ENVIRONENT = Development,ASP.NET Core 2.1应用程序也不会选择appsettings.Development.json。为什么?

时间:2018-10-04 03:18:23

标签: asp.net-core-2.1

所以我正在研究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区分大小写有关吗?谢谢!

更新:我的程序类和我的StartUp类(dotnet-cli工具通过运行“ dotnet new webapi”提供的默认ctor:

程序类:

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.
}

2 个答案:

答案 0 :(得分:2)

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

关于我的错误,实际上是由于一个非常愚蠢的错误:

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

此观察受到this GitHub issue的启发。

答案 1 :(得分:0)

保留三个嵌套文件,例如appsettings.jsonappsettings.Development.jsonappsettings.Production.json。然后是来自环境的环境名称。提及要在Startup.cs和bash设置环境中使用的文件,

$ export ASPNETCORE_ENVIRONMENT=Development

根据环境获取适当的文件。以此替换启动构造函数。

public class Startup
{
    public Startup(IConfiguration configuration, IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

    public IConfiguration Configuration { get; }

    // code removed...
}

在编写环境变量名称时请保持一致。 Linux环境变量名称区分大小写。