【问题标题】:How to access connectionString in your model?如何访问模型中的 connectionString?
【发布时间】:2017-10-05 09:48:29
【问题描述】:

如何访问模型中的 connectionString?我正在使用 Dapper。我发现的大多数解决方案都是使用 EntityFramework 而不是 Dapper。

这适用于我的本地机器(ma​​cOS):

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    Dapper.DapperHelper.ConnectionString = Configuration["ConnectionStrings:MyConnectionString"];
}

但是它不适用于 Linux Debian

在我运行应用程序后,Dapper.DapperHelper.ConnectionString 属性在 linux 上为空。

我的 appsettings.json 文件内容:

{
  "ConnectionStrings": {
    "MyConnectionString": "Server=ip; Database=db_name; User Id=my_user; Password=my_password; Pooling=false;" // SQL Server Authentication
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

困扰我的是,相同的代码在 macOS 上可以工作,但在 Debian (linux) 上却不工作。

【问题讨论】:

  • 1.不要使用静态,这比首先拥有一流 DI 的想法 2. 确保您还复制了您的 appsettings.json
  • 我试过非静态的,结果是一样的。也是的,我复制了我的 appsetting.json(整个“发布”目录)。
  • 你的 appsettings.json 里面真的有这些部分吗?它通常使用用户机密(它使用项目文件夹之外的 json 文件,因此不会意外提交给版本控制系统)
  • 我已将我的 appsettings.json 文件内容添加到我的问题中。是的,我在我的 linux 机器上打开了 appsettings.json 文件,这就是全部。
  • 所以你正在搜索“MyConnectionString”但你有一个“RutarConnectionString”?

标签: sql-server-2008 asp.net-core debian connection-string dapper


【解决方案1】:

在我的 Program.cs 文件中,我必须替换它:

public class Program
{
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                   .UseStartup<Startup>()
                   .UseUrls("http://localhost:8888")
                   .Build();
}

用这个:

public class Program
{
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((builderContext, config) =>
            {
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            })
            .UseStartup<Startup>()
            .UseUrls("http://localhost:8888")
            .Build();

}

等同于:

public static IWebHost BuildWebHost(string[] args)
{
            return WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((builderContext, config) =>
                {
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                })
                .UseStartup<Startup>()
                .UseUrls("http://localhost:8888")
                .Build();
}

使用声明列表:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

然后它可以在 Debian Linux 和 macOS 上运行。

这帮助我弄清楚了:https://joonasw.net/view/aspnet-core-2-configuration-changes

但是这部分:

WebHost.CreateDefaultBuilder ASP.NET Core 2.0 提供了一个不错的 为一个非常典型的配置创建一个方便的方法 应用。

而不是这样写:

...

你可以写:

...

不适用于 Debian Linux。这仅适用于 macOS。没有在 Windows 上测试过。

我也将此报告为错误:https://github.com/dotnet/corefx/issues/24446

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 2014-12-29
    • 1970-01-01
    • 2018-06-11
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-11
    相关资源
    最近更新 更多