无法针对MariaDB连接Docker中的.NET 6 API

suzh9iv8  于 2022-11-08  发布在  Docker
关注(0)|答案(1)|浏览(136)

对于一个新的项目,我尝试使用实体框架将我的asp.net 6 API与MariaDB连接起来。由于某些原因,似乎.NET无法看到数据库容器。
尝试建置项目时,我收到下列错误码:
系统操作无效异常:“已设定相关式存放区,但未指定要使用的DbConnection或连接字串。”
有办法解决这个问题吗?
Docker撰写:

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:

应用程序设置:

{
  "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"
  }
}

停靠文件:

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"]

数据库上下文:

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();
iezvtpos

iezvtpos1#

问题是:

optionsBuilder.UseMySQL(@_config["MariaDbConnectionString"]);

应该是
optionsBuilder.UseMySQL(@_config["ConnectionStrings:MariaDbConnectionString"]);

相关问题