Reputation: 1
For a new Project, I try to connect my asp.net 6 API against a MariaDB, using the entity framework. For some reasons, it seems like .NET cannot see the db container.
When trying to build the Project, I get the following Error Code:
System.InvalidOperationException: "A relational store has been configured without specifying either the DbConnection or connection string to use."
Is there a way of fixing this?
Docker Compose:
version: "3.7"
services:
mariadb:
image: mariadb:10.7
restart: always
ports:
- "3307:3306"
environment:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=family-chronicles-maria-root
- MYSQL_DATABASE=family-chronicles
- MYSQL_USER=family-chronicles
- MYSQL_PASSWORD=family-chronicles-maria
volumes:
- mariadb_data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin/phpmyadmin:5
ports:
- "8008:80"
environment:
- PMA_HOST=mariadb
- PMA_USER=family-chronicles
- PMA_PASSWORD=family-chronicles-maria
depends_on:
- mariadb
mongodb:
image: mongo
restart: always
ports:
- "61001:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: family-chronicles
MONGO_INITDB_ROOT_PASSWORD: family-chronicles-mongo
volumes:
- mongo_data:/data/db
mongo-express:
image: mongo-express
restart: always
ports:
- "61002:8081"
environment:
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_ADMINUSERNAME: family-chronicles
ME_CONFIG_MONGODB_ADMINPASSWORD: family-chronicles-mongo
depends_on:
- mongodb
family-chronicles-backend:
build:
context: .
dockerfile: src/Dockerfile
depends_on:
- mariadb
- mongodb
volumes:
mariadb_data:
mongo_data:
AppSettings:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MariaDbConnectionString": "Server=mariadb;Port=3306;Uid=family-chronicles;Pwd=family-chronicles-maria;Database=family-chronicles;"
},
"EntityFramework": {
"DefaultConnection": "MariaDbConnectionString"
}
}
DockerFile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["src/Family-Chronicles-Server.csproj", "Family-Chronicles-Server/"]
RUN dotnet restore "src/Family-Chronicles-Server.csproj"
COPY . .
WORKDIR "/src/Family-Chronicles-Server"
RUN dotnet build "Family-Chronicles-Server.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Family-Chronicles-Server.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Family-Chronicles-Server.dll"]
DbContext:
public class MariaDbContext : DbContext
{
private readonly IConfiguration _config;
public MariaDbContext(IConfiguration config)
{
_config = config;
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL(@_config["MariaDbConnectionString"]);
}
}
Program.cs
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var config = builder.Configuration;
var connectionString = config.GetConnectionString("MariaDbConnectionString");
var serverVersion = ServerVersion.Create(new Version(10, 4, 6), ServerType.MariaDb);
builder.Services.AddDbContext<MariaDbContext>(
dbContextOptions => dbContextOptions
.UseMySql(connectionString, serverVersion)
// The following three options help with debugging, but should
// be changed or removed for production.
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
);
builder.Services.AddMvc();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
using (var context = scope.ServiceProvider.GetService<MariaDbContext>())
{
if (context != null)
{
context.Database.EnsureCreated();
}
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger(x => x.SerializeAsV2 = true);
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Upvotes: 0
Views: 825
Reputation: 2600
here is the problem:
optionsBuilder.UseMySQL(@_config["MariaDbConnectionString"]);
It should be
optionsBuilder.UseMySQL(@_config["ConnectionStrings:MariaDbConnectionString"]);
Upvotes: 1