when connecting database 404 not found error

I created a db from docker and created db in mysql. After that I build a mini backend with ef core and asp net for homework but I couldn't manage to connect it. My connection string is correct
68 Replies
Pobiega
Pobiega6mo ago
are you using Docker Desktop on a windows PC?
only you know
only you knowOP6mo ago
im using docker desktop on mac
Pobiega
Pobiega6mo ago
okay. And the database container shows up as expected in the docker desktop GUI? and has status "running" etc?
only you know
only you knowOP6mo ago
yes it's running also I managed to connect mysql from terminal while running in docker
Pobiega
Pobiega6mo ago
with the terminal on the host machine, right? using mysql cli
only you know
only you knowOP6mo ago
this packages are true right
No description
only you know
only you knowOP6mo ago
yes.
Pobiega
Pobiega6mo ago
Im not sure I'd use MySql.EntityFrameworkCore
only you know
only you knowOP6mo ago
what would you use I'll try
Pobiega
Pobiega6mo ago
Pomelo.EntityFrameworkCore.MySql
only you know
only you knowOP6mo ago
let me trry
Pobiega
Pobiega6mo ago
in general, mysql isnt a very good database, and the official connector has had issues in the past im not sure if its still true
only you know
only you knowOP6mo ago
I can change db now it's okay if you say not very good what can I use else
Pobiega
Pobiega6mo ago
postgres is very good Npgsql.EntityFrameworkCore.PostgreSQL is the EFcore connector for that
only you know
only you knowOP6mo ago
I gotta create new container for postgres but I want to know that problem is mysql or my codes
only you know
only you knowOP6mo ago
after pomelo mysql package I got this error
No description
only you know
only you knowOP6mo ago
I think its not about package too I am getting not found error again
Pobiega
Pobiega6mo ago
can you show a screenshot from docker desktop container overview window, and your connection string?
only you know
only you knowOP6mo ago
No description
only you know
only you knowOP6mo ago
running one is my container "server=localhost;port=3306;database=ENSAR;uid=root;password=ensarerdeve2001;" its my connection string sorry about the late answer what do you say it might be the code part?
Pobiega
Pobiega6mo ago
Hard to say.
only you know
only you knowOP6mo ago
can I share the db context and program.cs with you
Pobiega
Pobiega6mo ago
sure. $code
MODiX
MODiX6mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
only you know
only you knowOP6mo ago
using Microsoft.EntityFrameworkCore;
using WebBackend.Model;

namespace WebBackend.Db
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var serverVersion = new MySqlServerVersion(new Version(8, 0, 2));
optionsBuilder.UseMySql("server=localhost;port=3306;database=ENSAR;uid=root;password=ensarerdeve2001;", serverVersion);
}


public DbSet<Product> Products { get; set; }
}
}
using Microsoft.EntityFrameworkCore;
using WebBackend.Model;

namespace WebBackend.Db
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var serverVersion = new MySqlServerVersion(new Version(8, 0, 2));
optionsBuilder.UseMySql("server=localhost;port=3306;database=ENSAR;uid=root;password=ensarerdeve2001;", serverVersion);
}


public DbSet<Product> Products { get; set; }
}
}
using WebBackend.Repository;
using WebBackend.Db;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IProductRepository,ProductRepository>();
builder.Services.AddControllers();
builder.Services.AddDbContext<AppDbContext>(options =>
{
var serverVersion = new MySqlServerVersion(new Version(8, 0, 2));
options.UseMySql("server=localhost;port=3306;database=ENSAR;uid=root;password=ensarerdeve2001;", serverVersion);
});
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

//app.UseHttpsRedirection();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
using WebBackend.Repository;
using WebBackend.Db;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IProductRepository,ProductRepository>();
builder.Services.AddControllers();
builder.Services.AddDbContext<AppDbContext>(options =>
{
var serverVersion = new MySqlServerVersion(new Version(8, 0, 2));
options.UseMySql("server=localhost;port=3306;database=ENSAR;uid=root;password=ensarerdeve2001;", serverVersion);
});
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

//app.UseHttpsRedirection();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
don't mind the default weather thing
Pobiega
Pobiega6mo ago
why do I get the distinct feeling that I've helped you before with this? The name "ENSAR" in all caps...
only you know
only you knowOP6mo ago
that's the db name. That's my first time with ef core
only you know
only you knowOP6mo ago
No description
only you know
only you knowOP6mo ago
you see any obvious mistakes?
Pobiega
Pobiega6mo ago
why are you declaring the provider twice? you do it both in program.cs with AddDbContext (correct) and in the context itself in OnConfiguring (bad wrong delete!)
only you know
only you knowOP6mo ago
so onconfiguring is unnecessary
Pobiega
Pobiega6mo ago
yes does "ensar" mean anything special? I have definately seen that database name before a few months ago helping someone on here
only you know
only you knowOP6mo ago
its my name. that was me probably. I was working with ado.net that time db is running but still getting not found error.
Pobiega
Pobiega6mo ago
can you show me the command you run in the terminal to connect to it? from the host machine
only you know
only you knowOP6mo ago
% dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5150
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/ensarerdeve/Documents/VSC Projects/WebProject/WebBackend
^Cinfo: Microsoft.Hosting.Lifetime[0]
Application is shutting down...
% dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5150
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/ensarerdeve/Documents/VSC Projects/WebProject/WebBackend
^Cinfo: Microsoft.Hosting.Lifetime[0]
Application is shutting down...
you meant this?
Pobiega
Pobiega6mo ago
no, the mysql cli
only you know
only you knowOP6mo ago
docker exec -it mysql-okul mysql -uroot -pensarerdeve2001
docker exec -it mysql-okul mysql -uroot -pensarerdeve2001
Pobiega
Pobiega6mo ago
so, thats not the same thats using docker to tunnel into the container its not connecting from the host
only you know
only you knowOP6mo ago
I didn't tried to connect directly mysql from terminal
Pobiega
Pobiega6mo ago
well you need to verify that the container can be reached from the host
only you know
only you knowOP6mo ago
how can I verify
Pobiega
Pobiega6mo ago
you could get something like heidisql or mysql workbench or something and try to connect using that
only you know
only you knowOP6mo ago
so I should always have a program like that right for verify that I can connect except from docker
Pobiega
Pobiega6mo ago
¯\_(ツ)_/¯ always and always
only you know
only you knowOP6mo ago
what do you say dbeaver is it useful
Pobiega
Pobiega6mo ago
if you are having issues connecting, the first step should be to verify that you CAN connect at all never heard of it. I have not used mysql in many many years (because its a garbage database)
only you know
only you knowOP6mo ago
oh gotcha. can you tell me the reason why it's garbage. I want to get knowledge about it
Pobiega
Pobiega6mo ago
$whynotmysql
MODiX
MODiX6mo ago
https://dev.mysql.com/doc/refman/8.0/en/insert.html
Inserting NULL into a column that has been declared NOT NULL. For multiple-row INSERT statements or INSERT INTO ... SELECT statements, the column is set to the implicit default value for the column data type. This is 0 for numeric types, the empty string ('') for string types, and the “zero” value for date and time types. INSERT INTO ... SELECT statements are handled the same way as multiple-row inserts because the server does not examine the result set from the SELECT to see whether it returns a single row. (For a single-row INSERT, no warning occurs when NULL is inserted into a NOT NULL column. Instead, the statement fails with an error.) Setting a numeric column to a value that lies outside the column range. The value is clipped to the closest endpoint of the range. Assigning a value such as '10.34 a' to a numeric column. The trailing nonnumeric text is stripped off and the remaining numeric part is inserted. If the string value has no leading numeric part, the column is set to 0. Inserting a string into a string column (CHAR, VARCHAR, TEXT, or BLOB) that exceeds the column maximum length. The value is truncated to the column maximum length. Inserting a value into a date or time column that is illegal for the data type. The column is set to the appropriate zero value for the type.
Pobiega
Pobiega6mo ago
$whynotmysql2
MODiX
MODiX6mo ago
oracle
Pobiega
Pobiega6mo ago
hah, that last one is new 😄 but also not wrong. Oracle is a pretty terrible company the very best reason was a blog article written by Steinar Gunderson that seems to have been deleted, but here is a quote
After five years in Oracle’s MySQL team, Steinar H. Gunderson resigned a few days ago. On the same day, he dropped the bomb on his blog: let me point out something that I’ve been saying both internally and externally for the last five years (although never on a stage—which explains why I’ve been staying away from stages talking about MySQL): MySQL is a pretty poor database, and you should strongly consider using Postgres instead1.
only you know
only you knowOP6mo ago
uhh I see btw should I connect to db from docker ip or localhost? or both same? now I will connect through workbench
Pobiega
Pobiega6mo ago
localhost or 127.0.0.1 are the same thing if connecting from the host machine, either should be fine
only you know
only you knowOP6mo ago
I managed to connect from host machine so high chance the problem code part
Pobiega
Pobiega6mo ago
what exactly is the error message?
only you know
only you knowOP6mo ago
there is no message directly it just says 404 not found
Pobiega
Pobiega6mo ago
no way thats a http error I highly doubt your database connection would "throw" a 404 not found I found the article reposted on a chinese blog-aggregator https://www.cnblogs.com/dbadaily/p/mysql-or-postgresql.html
only you know
only you knowOP6mo ago
I'll try to change mysql now maybe the problem itself is mysql
Pobiega
Pobiega6mo ago
it really isnt its something else going wrong now, not the database connection
only you know
only you knowOP6mo ago
I started to vscode 2-3 days ago am I missing some kind packages maybe
Pobiega
Pobiega6mo ago
no, the 404 error is not from your database connection
only you know
only you knowOP6mo ago
I have warn something like
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
only you know
only you knowOP6mo ago
BlazeBin - ymqktkoipssp
A tool for sharing your source code with the world!
only you know
only you knowOP6mo ago
can you check my controller
Pobiega
Pobiega6mo ago
return NotFound(); would give a 404 not found thats literally what that line of code does set a breakpoint in the route you are trying to hit, and then hit it step through your functions, see whats happening
only you know
only you knowOP6mo ago
oh I've solved the problem by adding
app.UseAuthorization();
app.MapControllers()
app.UseAuthorization();
app.MapControllers()
added these and after when I try to get method it started to response and said no table and I did the migration
Want results from more Discord servers?
Add your server