【问题标题】:.Net CORE Dapper Connection String?.Net CORE Dapper 连接字符串?
【发布时间】:2016-10-02 12:17:18
【问题描述】:

我正在设置我的第一个 .NET Core 应用程序。我将为 ORM 使用 Dapper (1.50.0-rc2)。

我已将以下内容添加到我的 appsettings.json 文件中。

"Data": {
    "DefaultConnection": {
        "ConnectionString": "user id=exampleusername;password=examplepassword;Data Source=db.example.com;Database=exampledb;"
    }
},

我对如何获取 ConnectionString 的值感到困惑。由于 .NET Core 太新了,网上的例子比比皆是,但似乎没有一个真正涵盖这一点。

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc


    【解决方案1】:

    我的 GitHub repository 上有一个用于 .NET 核心的示例控制台应用程序

    设置阶段

    var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    

    构建阶段

    Configuration = builder.Build();

    使用阶段

    Configuration.GetConnectionString("DefaultConnection")

    您可以将此值用于Dapper

    附言

    您需要在project.json 中添加 3 个依赖项

    "Microsoft.Extensions.Configuration": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final"
    

    更新

    具体解决方案

    制作配置静态属性并添加私有设置器

    public static IConfigurationRoot Configuration { get; private set; }

    并更改您的扩展名

    namespace GamesCore.Extensions 
    {
        public class ScoreExtensions 
        { 
            private static string dataConnectionString = Startup.Configuration.GetConnectionString("DefaultConnection"); 
        } 
    }
    

    对于 .NET Core 2.0,一切都是一样的,只有项目文件发生了变化,因此您需要使用以下包:

      <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.2" />
        <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.2" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.2" />
      </ItemGroup>
    

    【讨论】:

    • 我在 Startup.cs 的 Startup 方法中有“安装阶段”和“构建阶段”(它们已经在 Visual Studio 模板中)。我将缺少的依赖项添加到我的 project.json 中。但是在“使用阶段”我得到“当前上下文中不存在名称'配置'我尝试添加一个 using Microsoft.Extensions.Configuration 但导致'类型或命名空间名称'GetConnectionString'不存在'错误。
    • 抱歉,按回车键太快了。请参阅上面的更新评论。
    • @eat-sleep-code 你想在哪里使用Configuration
    • 在一个扩展类中; namespace GamesCore.Extensions { public class ScoreExtensions { private static string dataConnectionString = Configuration.GetConnectionString("DefaultConnection"); } }
    • @niico 为 .net core 2 添加了包
    【解决方案2】:

    我的演练:

    1. appsettings.json 中添加 ConnectionStrings 部分:

      "ConnectionStrings": {
         "cs1": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;",
         "cs2": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;",
         "cs3": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;"
      },
      
    2. 创建代表连接字符串部分的类:

      public class ConnectionStringList
      {
          public string cs1 { get; set; }
          public string cs2 { get; set; }
          public string cs3 { get; set; }
      }
      
    3. 打开Startup.cs,将上述配置类添加到ConfigureServices中的服务集合中:

      public void ConfigureServices(IServiceCollection services)
      {
          ...
          services.AddOptions();
          services.Configure<ConnectionStringList>(Configuration.GetSection("ConnectionStrings"));
      }
      
    4. IOptions 注入您的控制器/服务等,并从 Value 属性中检索您的连接字符串值:

      public SampleController(IOptions<ConnectionStringList> connectionStrings)
      {
          string cs1 = connectionStrings.Value.cs1;
          ...
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-06
      • 2019-01-07
      • 1970-01-01
      • 2018-05-28
      • 2021-10-29
      • 2019-05-08
      • 1970-01-01
      相关资源
      最近更新 更多