Chibuzo
Chibuzo

Reputation: 6117

Setup Database Context for Entity Framework 6 using Environmental Variables

I'm having challenges connecting to a remote MySQL database server from an ASP.NET WebAPI 2 app using Entity Framework code first, on Azure.

It works fine on my local server.

Here is the connection string from Web.config

<connectionStrings>
    <add name="AngryUsersContext" providerName="MySql.Data.MySqlClient" connectionString="server=mydbhost;port=3306;database=test_db;uid=gq_user;password=*****" />
</connectionStrings>

This is part of the context 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; }

    ...

I did set up a connection string on application settings. I tried Type: Custom and MySQL but none worked. enter image description here

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.

Any help would be appreciated.

Upvotes: 1

Views: 456

Answers (1)

Joey Cai
Joey Cai

Reputation: 20097

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.

When publish your project from Visual Studio and click up this option Use this connection string at runtime(update destination web.config) then it will Cover the local connectionstring with connectionstring on portal you provided above.

enter image description here

For more details, you could refer to this article.

Upvotes: 1

Related Questions