从 docker 映像创建的部分数据库

Partial database created from docker image

提问人:Amokachi 提问时间:11/14/2023 更新时间:11/14/2023 访问量:19

问:

我有一个dockerized应用程序,当我在本地计算机上运行时可以正常工作,但是当 我使用“docker-compose up”,数据库仅部分创建。 数据库包含 63 个表,当使用 docker-compose 运行 app 时,仅创建 6 个表。没有错误消息。

这是我的代码:

public static async Task Main(string[] args)
{
    var configuration = new ConfigurationBuilder()
        .Build();

    var host = CreateHostBuilder(args).Build();
    await CreateDbIfNotExists(host);

    host.Run();
}

private static async Task CreateDbIfNotExists(IHost host)
{
    using (var scope = host.Services.CreateScope())
    {
        var services = scope.ServiceProvider;

        try
        {
            var context = services.GetRequiredService<MeerkatContext>();
            var env = services.GetRequiredService<IWebHostEnvironment>();
            await context.Database.EnsureCreatedAsync();
            await DBSeeder.SeedAsync(context,  env);
        }
        catch (Exception ex)
        {
            var logger = services.GetRequiredService<ILogger<Program>>();
            logger.LogError(ex, "An error occurred creating the DB.");
        }
    }

上下文:

public class MeerkatContext : IdentityDbContext<AppUser>
{
    public MeerkatContext(DbContextOptions<MeerkatContext> options) : base(options)
    {
    }

    public DbSet<PropertyType> PropertyType { get; set; }
    public DbSet<TransactionType> TransactionType { get; set; }
    public DbSet<Country> Country { get; set; }
    public DbSet<Log> Log { get; set; }
    public DbSet<UserProfile> UserProfile { get; set; }
    public DbSet<Category> Category { get; set; }
    public DbSet<CategoryTranslation> CategoryTranslation { get; set; }
    public DbSet<City> City { get; set; }

    public DbSet<Property> Property { get; set; }
    public DbSet<RealEstate> RealEstate { get; set; }
    public DbSet<LeasePeriod> LeasePeriod { get; set; }
    public DbSet<LeaseLengthUnit> LeaseLengthUnit { get; set; }
    public DbSet<LeaseLengthUnitTranslation> LeaseLengthUnitTranslation { get; set; }
    public DbSet<Listing> Listing { get; set; }
    public DbSet<AppUser> AppUser { get; set; }
    public DbSet<Advertiser> Advertiser { get; set; }

    public DbSet<PropertyTotal> PropertyTotal { get; set; }
    public DbSet<MessageType> MessageType { get; set; }
    public DbSet<View> View { get; set; }
    public DbSet<HomeGallery> HomeGallery { get; set; }
    public DbSet<UrgentListing> UrgentListing { get; set; }
    public DbSet<PaidListing> PaidListing { get; set; }
    public DbSet<ClassicServicePlan> ClassicServicePlan { get; set; }

    public DbSet<PremiumServicePlan> PremiumServicePlan { get; set; }
    public DbSet<PrestigeServicePlan> PrestigeServicePlan { get; set; }
    public DbSet<AvailabilityPeriod> AvailabilityPeriod { get; set; }
    public DbSet<AdvertisementFee> AdvertisementFee { get; set; }
    public DbSet<ServiceType> ServiceType { get; set; }

    public DbSet<Advertisement> Advertisement { get; set; }
    public DbSet<Service> Service { get; set; }
    public DbSet<ServiceFee> ServiceFee { get; set; }
    public DbSet<WorkQueue> WorkQueue { get; set; }
    public DbSet<Payment> Payment { get; set; }

    public DbSet<AdCampaignDuration> AdCampaignDuration { get; set; }
    public DbSet<AdLocationOnPage> AdLocationOnPage { get; set; }
    public DbSet<ServiceTypeTarget> ServiceTypeTarget { get; set; }
    public DbSet<Language> Language { get; set; }
    public DbSet<LanguageTranslation> LanguageTranslation { get; set; }
    public DbSet<AccountType> AccountType { get; set; }
    public DbSet<AccountTypeTranslation> AccountTypeTranslation { get; set; }

    public DbSet<SortOrder> SortOrder { get; set; }
    public DbSet<StripePaymentMetadata> StripePaymentMetadata { get; set; }
    public DbSet<CommunicationTemplate> CommunicationTemplate { get; set; }
    public DbSet<EmailTemplate> EmailTemplate { get; set; }
    public DbSet<SmsTemplate> SmsTemplate { get; set; }

    public DbSet<CommunicationType> CommunicationType { get; set; }
    public DbSet<TransactionTypeTranslation> TransactionTypeTranslation { get; set; }
    public DbSet<PropertyTypeTranslation> PropertyTypeTranslation { get; set; }
    public DbSet<AdLocationOnPageTranslation> AdLocationOnPageTranslation { get; set; }
    public DbSet<ExchangeRate> ExchangeRate { get; set; }
    public DbSet<CountryTranslation> CountryTranslation { get; set; }
    public DbSet<ServiceTypeTranslation> ServiceTypeTranslation { get; set; }
    public DbSet<SortOrderTranslation> SortOrderTranslation { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        EFHelper.ConfigureModelBuider(modelBuilder);
    }
}

使用 docker-compose 运行应用时创建的数据库

enter image description here

我的dockerfile

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["Meerkat.WebService/Meerkat.WebService.csproj", "Meerkat.WebService/"]
COPY ["Meerkat.Application/Meerkat.Application.csproj", "Meerkat.Application/"]
COPY ["Meerkat.Domain/Meerkat.Domain.csproj", "Meerkat.Domain/"]
COPY ["Meerkat.Infrastructure/Meerkat.Infrastructure.csproj", "Meerkat.Infrastructure/"]
RUN dotnet restore "Meerkat.WebService/Meerkat.WebService.csproj"
COPY . .
WORKDIR "/src/Meerkat.WebService"
RUN dotnet build "Meerkat.WebService.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Meerkat.WebService.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Meerkat.WebService.dll"]

注意:在我的本地正常工作的映像是我在 dockerhub 上部署的映像

感谢您的任何见解

. docker .net-core

评论


答: 暂无答案