使用环境变量为 Entity Framework 6 设置数据库上下文
Setup Database Context for Entity Framework 6 using Environmental Variables
我在 Azure 上首先使用 Entity Framework 代码从 ASP.NET WebAPI 2 应用程序连接到远程 MySQL 数据库服务器时遇到了挑战。
它在我的本地服务器上运行良好。
这是来自 Web.config
的连接字符串
<connectionStrings>
<add name="AngryUsersContext" providerName="MySql.Data.MySqlClient" connectionString="server=mydbhost;port=3306;database=test_db;uid=gq_user;password=*****" />
</connectionStrings>
这是上下文的一部分class
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class AngryUsersContext : DbContext
{
public AngryUsersContext() : base("name=AngryUsersContext")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<AngryUsersContext, AngryUsers.Migrations.Configuration>());
}
public System.Data.Entity.DbSet<AngryUsers.Models.Complaint> Complaints { get; set; }
...
我确实在应用程序设置中设置了连接字符串。我试过 Type: Custom 和 MySQL 但 none 有效。
我认为我的代码应该从环境变量加载连接字符串以使用和连接到数据库。我不知道如何修改我的上下文 class 来实现它。
如有任何帮助,我们将不胜感激。
I think my code is supposed to load the connection string from environmental variables to use and connect to the database. I don't know how to modify my context class to achieve that.
当您从 Visual Studio 发布您的项目并单击此选项 Use this connection string at runtime(update destination web.config)
时,它将 用您在上面提供的门户 上的连接字符串覆盖本地连接字符串。
更多细节,你可以参考这篇article。
我在 Azure 上首先使用 Entity Framework 代码从 ASP.NET WebAPI 2 应用程序连接到远程 MySQL 数据库服务器时遇到了挑战。
它在我的本地服务器上运行良好。
这是来自 Web.config
的连接字符串<connectionStrings>
<add name="AngryUsersContext" providerName="MySql.Data.MySqlClient" connectionString="server=mydbhost;port=3306;database=test_db;uid=gq_user;password=*****" />
</connectionStrings>
这是上下文的一部分class
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class AngryUsersContext : DbContext
{
public AngryUsersContext() : base("name=AngryUsersContext")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<AngryUsersContext, AngryUsers.Migrations.Configuration>());
}
public System.Data.Entity.DbSet<AngryUsers.Models.Complaint> Complaints { get; set; }
...
我确实在应用程序设置中设置了连接字符串。我试过 Type: Custom 和 MySQL 但 none 有效。
我认为我的代码应该从环境变量加载连接字符串以使用和连接到数据库。我不知道如何修改我的上下文 class 来实现它。
如有任何帮助,我们将不胜感激。
I think my code is supposed to load the connection string from environmental variables to use and connect to the database. I don't know how to modify my context class to achieve that.
当您从 Visual Studio 发布您的项目并单击此选项 Use this connection string at runtime(update destination web.config)
时,它将 用您在上面提供的门户 上的连接字符串覆盖本地连接字符串。
更多细节,你可以参考这篇article。