Deploy ASP.Net Core with MySql to Heroku
Tags: csharp, dotnet, heroku, tutorial
Published: 2021-02-10 12:59:42

Heroku is a great tool to host personal projects and there's a free tier. However, .Net Core is not supported for now but, there's a work around this.

### Prerequisites
* A Heroku account - https://signup.heroku.com/
* Install the Heroku CLI - https://devcenter.heroku.com/articles/heroku-cli
* git enabled


1. Create a new app on the Heroku website 
    ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fdd8mekuxf76p9o4utdo.png)
and ensure the app name is unique.

2. Open the terminal on your system, change directory to your core project and log into Heroku account.
> heroku login

3. Add the Heroku Git remote
   > heroku git:remote -a {your app name}

4. We need a dotnet buildpack because, dotnet is not supported.
   For this tutorial, I used and my .net core version is 3.1.3  https://elements.heroku.com/buildpacks/jincod/dotnetcore-buildpack 

     Use this command if your .net core version is the same as mine
     > heroku buildpacks:set https://github.com/jincod/dotnetcore-buildpack\#v3.1.3

     If you are on a .net 5. You can use this command
     > heroku buildpacks:set jincod/dotnetcore
     
     Ensure you are on heroku-18 stack 
     > heroku stack:set heroku-18
     

5. To a database resource: Navigate to the resources tab then search for ClearDB MySql and select the free version.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/37jpfxwk7efyvgjj9itf.png)

To get the connection string, you need to navigate to the settings and click on Reveal Config Vars.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/64wad6v5915l90fwqksn.png)

Also, add ASPNETCORE_ENVIRONMENT config and set it to Production.

ClearDb connection string is usually in this format :
> mysql://username:password@host/databasename?reconnect=true

you can use the code snippet below to build the connection string for EF core

```
services.AddDbContext<DbContext>(x =>
            {

                var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
                string connStr;

                if (env == "Development")
                {
                    connStr = Configuration.GetConnectionString("DemoConnection");


                }
                else
                {
                    // Use connection string provided at runtime by Heroku.
                    var connUrl = Environment.GetEnvironmentVariable("CLEARDB_DATABASE_URL");

                    connUrl = connUrl.Replace("mysql://", string.Empty);
                    var userPassSide = connUrl.Split("@")[0];
                    var hostSide = connUrl.Split("@")[1];

                    var connUser = userPassSide.Split(":")[0];
                    var connPass = userPassSide.Split(":")[1];
                    var connHost = hostSide.Split("/")[0];
                    var connDb = hostSide.Split("/")[1].Split("?")[0];

              
                    connStr = $"server={connHost};Uid={connUser};Pwd={connPass};Database={connDb}";



                }

                x.UseMySql(connStr);

            });
```



**Now we are set to publish to heroku. Use the command below to achieve this**

> git push heroku master 

Once publishing has been completed, your app url can be found in the settings tab.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/calo27xo5rwoz7g6gm6y.png)