ASP.Net MVC 6 Deployment

KKapatiD12/2/2022
Does anyone know how to deploy and ASP.Net MVC in Railway with a MySQL database?
Rroot12/2/2022
You should be able to add a MySQL database service and then deploy your code as usual.
Rroot12/2/2022
(Railway has built-in .NET support through Nixpacks, so you shouldn't need a Dockerfile or anything: https://nixpacks.com/docs/providers/csharp)
KKapatiD12/2/2022
Cool will check it out
KKapatiD12/2/2022
I was able to able to deploy it but it fails to connect to my MySQL. I also noticed that it did not automatically generated a table when I added my Github repo
Rroot12/2/2022
N/A
Rroot12/2/2022
N/AS
Rroot12/2/2022
N/A
Rroot12/2/2022
Are you using the DATABASE_URL env var to connect?
KKapatiD12/2/2022
Not really sure where to put that in my project
Rroot12/2/2022
Are you using EF6?
Rroot12/2/2022
the env var DATABASE_URL should be passed as the connection string
Rroot12/2/2022
Something like
MySqlConnection conn = new MySqlConnection(System.Environment.GetEnvironmentVariable("DATABASE_URL"));
KKapatiD12/2/2022
do you mean entity framework?
Rroot12/2/2022
Yes
KKapatiD12/2/2022
yup it saya 6.0.10
KKapatiD12/2/2022
says*
Rroot12/2/2022
Where are you initializing your DB connection?
KKapatiD12/2/2022
no sure about that because I always use code gen in Visual Studio do you want to take a look at my github repo? its just a todo app for practice
Rroot12/2/2022
Sure
Rroot12/2/2022
1. You're trying to use SQL Server right now
Rroot12/2/2022
options.UseSqlServer(builder.Configuration.GetConnectionString("TodoWebAppContext") ?? throw new InvalidOperationException("Connection string 'TodoWebAppContext' not found.")));
KKapatiD12/2/2022
oh
KKapatiD12/2/2022
What do I need to change?
Rroot12/2/2022
First, add the MySql.Data.EntityFramework package
Rroot12/2/2022
Then replace UseSqlServer with UseMySql
Rroot12/2/2022
And replace the GetConnectionString call with System.Environment.GetEnvironmentVariable("DATABASE_URL")
Rroot12/2/2022
And see if that works
Rroot12/2/2022
(I'm afraid I haven't worked with .NET that much)
Rroot12/2/2022
All in Program.cs
KKapatiD12/2/2022
Any help is appreciated and will now try your suggestions
KKapatiD12/3/2022
I noticed that in Railway they have a different version of MySQL. How do I turn this into their format?

CREATE TABLE [dbo].[Todo] (
    [Id]          INT            IDENTITY (1, 1) NOT NULL,
    [Title]       NVARCHAR (MAX) NULL,
    [Done]        BIT            NOT NULL,
    [CreatedDate] DATETIME2 (7)  NOT NULL,
    CONSTRAINT [PK_Todo] PRIMARY KEY CLUSTERED ([Id] ASC)
);
KKapatiD12/3/2022
I tried doing this steps but nothing worked so I ended up with this code instead even though it still does not work.
Program.cs

using Microsoft.EntityFrameworkCore;
using TodoWebApp.Data;

var builder = WebApplication.CreateBuilder(args);

if (builder.Environment.IsDevelopment())
{
    builder.Services.AddDbContext<TodoWebAppContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("TodoWebAppContext") ?? throw new InvalidOperationException("Connection string 'TodoWebAppContext' not found.")));
}
else if (builder.Environment.IsProduction())
{
    builder.Services.AddDbContext<TodoWebAppContext>(options =>
        options.UseSqlServer(Environment.GetEnvironmentVariable("DATABASE_URL")!));
}

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
KKapatiD12/3/2022
I also needed to change my TodoWebApp.csproj file into this:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <!--Production-->
  <PropertyGroup Condition="'$(Configuration)' != '' AND '$(Configuration)' != 'Debug'">
    <InvariantGlobalization>true</InvariantGlobalization>
    <EnvironmentName>Production</EnvironmentName>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.10">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
  </ItemGroup>

</Project>
KKapatiD12/3/2022
This is the error I get in my body when my app is deployed in Railway. I think that Environment.GetEnvironmentVariable("DATABASE_URL") is what keeps me from accessing my env variable even though I already created DATABASE_URL in my Variables.
Rroot12/3/2022
1. Please use UseMySql instead of UseSqlServer
Rroot12/3/2022
2. Here's an updated query; the one you sent isn't MySQL, it's SQL Server:
CREATE TABLE Todo (
  Id           INT       NOT NULL AUTO_INCREMENT,
  Title        MEDIUMTEXT,
  Done         BOOL      NOT NULL,
  CreatedDate  DATETIME  NOT NULL
  PRIMARY KEY (Id)
)
Rroot12/3/2022
3. GetEnvironmentVariable isn't the problem.
Rroot12/3/2022
4. Remove Microsoft.EntityFrameworkCore.SqlServer and replace it with MySql.Data.EntityFramework
Rroot12/3/2022
(in the csproj)
Rroot12/3/2022
5. Set DOTNET_ENVIRONMENT=Production on Railway.
KKapatiD12/4/2022
Ok I will try again
KKapatiD12/4/2022
Oh up until now I thought it was my version that is different
KKapatiD12/4/2022
Everything works now 😅. Thanks again for pointing out to get the env variables of Railway!

Program.cs
var connectionString = "Server=localhost;Database=test;User=root;Password=;";
var serverVersion = new MySqlServerVersion(new Version(8, 0, 29));

...

if (builder.Environment.IsDevelopment())
{
    builder.Services.AddDbContext<TodoWebAppContext>(options =>
        //options.UseMySQL(builder.Configuration.GetConnectionString("TodoWebAppContext") ?? throw new InvalidOperationException("Connection string 'TodoWebAppContext' not found.")));
        options.UseMySql(connectionString, serverVersion));
}
else if (builder.Environment.IsProduction())
{
    var server = Environment.GetEnvironmentVariable("MYSQLHOST");
    var db = Environment.GetEnvironmentVariable("MYSQLDATABASE");
    var port = Environment.GetEnvironmentVariable("MYSQLPORT");
    var user = Environment.GetEnvironmentVariable("MYSQLUSER");
    var pass = Environment.GetEnvironmentVariable("MYSQLPASSWORD");

    var prodConnection = $"server={server};database={db};port={port};user={user};password={pass};";

    builder.Services.AddDbContext<TodoWebAppContext>(options =>
        options.UseMySql(prodConnection, serverVersion));
}

...
KKapatiD12/4/2022
TodoWebApp.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <!--Production-->
  <PropertyGroup Condition="'$(Configuration)' != '' AND '$(Configuration)' != 'Debug'">
    <InvariantGlobalization>true</InvariantGlobalization>
    <EnvironmentName>Production</EnvironmentName>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.9">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
  </ItemGroup>

</Project>
KKapatiD12/4/2022
I used Pomelo.EntityFrameworkCore.MySql since MySql.Data.EntityFramework is deprecated.
Rroot12/4/2022
Great! No problem!