asp.net-core - 从 ASP.NET CORE 中的 appsettings.json 文件读取 Entity Framework 6 连接字符串

标签 asp.net-core entity-framework-6 appsettings

我有一个 app.config xml 文件来保存 Entity Framework 的连接字符串,如下例所示:

<configuration>
    <connectionStrings>
        <add name="MyEntities" connectionString="metadata=res://*/EntityFramework.MyEntities.csdl|res://*/EntityFramework.MyEntities.ssdl|res://*/EntityFramework.MyEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=myserver;initial catalog=Example;persist security info=True;user id=myId;password=myPassword;multipleactiveresultsets=false;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>
</configuration>

但是我使用的是 ASP.NET Core,我使用环境变量管理多个服务器。所以我需要将此连接字符串保存在我的 appsettings.{enviroment}.json 中,读取此连接并将其设置为属性 Entity Framework 上下文。

最佳答案

我认为您正在寻找类似以下结构的东西。

  "ConnectionStrings": {
    "MyEntities": "metadata=res://*/EntityFramework.MyEntities.csdl|res://*/EntityFramework.MyEntities.ssdl|res://*/EntityFramework.MyEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=myserver;initial catalog=Example;persist security info=True;user id=myId;password=myPassword;multipleactiveresultsets=false;application name=EntityFramework"
  }

关于asp.net-core - 从 ASP.NET CORE 中的 appsettings.json 文件读取 Entity Framework 6 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41860338/

相关文章:

css - 在 ASP.NET Core 2.2 应用程序中使用自定义 .otf 字体

c# - 使用 Unity 时出现 Entity Framework 错误

entity-framework - Entity Framework 6.0.2 的源代码

c# - 如何让 .Net Core 3.1 查看我的应用程序设置文件

asp.net - 什么是 appSettings 键有效字符?

ASP.NET 5 : What is the recommended approach to catch and log all unhandled exceptions?

c# - 列出多个组下的 API 方法

c# - WPF MVVM 与 Razor 页面 MVVM

c# - 我必须在 Entity Framework 6 中为 DbContext 使用 "Using" block 吗

asp.net - appSettings key 中是否可以有一个&符号?