Bayash Bakytbekov
Bayash Bakytbekov

Reputation: 1

Tables aren't auto-creating in PgAdmin 4 and migrations aren't happening. Any advice?

I'm using Marten with PostgreSQL in an ASP.NET Core application. I've configured Marten with AutoCreate.All to auto-create schemas, but tables aren't being created on first connection. Here's my Marten configuration:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using Marten;
using Weasel.Core;
using baumkataster.Models.Baum;
using baumkataster.Models.Auftrag;
using baumkataster.Kontrolle;
using baumkataster.Models;
using baumkataster.Models.Gehölzdaten;
using baumkataster.Models.Koordinatentyp;

var builder = WebApplication.CreateBuilder(args);

// Services configuration
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddLogging();

// Marten configuration
builder.Services.AddMarten(options =>
{
    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
    options.Connection(connectionString);
    options.AutoCreateSchemaObjects = AutoCreate.All;

    // Document mappings
    options.Schema.For<Baum>().Identity(x => x.Id);
    options.Schema.For<Auftrag>().Identity(x => x.Id);
    options.Schema.For<Baumart>().Identity(x => x.Id);
    options.Schema.For<Gehölzdaten>().Identity(x => x.Id);
    options.Schema.For<Koordinatentyp>(); 
    options.Schema.For<Baumkontrolle>().ForeignKey<Auftrag>(x => x.AuftragId);
    options.Schema.For<Baumpflege>().ForeignKey<Auftrag>(x => x.AuftragId);
});

var app = builder.Build();

// HTTP pipeline configuration
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

I've ensured that the DefaultConnection in my appsettings.json file points to the correct PostgreSQL database. What could be the issue? How can I ensure Marten creates the necessary tables on first connection?

Upvotes: 0

Views: 154

Answers (1)

codingman123
codingman123

Reputation: 1

You can try to add this

.ApplyAllDatabaseChangesOnStartup();

at the end of the AddMarten method. E.i:

// Marten configuration
builder.Services.AddMarten(options =>
{
    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
    options.Connection(connectionString);
    options.AutoCreateSchemaObjects = AutoCreate.All;

    // Document mappings
    options.Schema.For<Baum>().Identity(x => x.Id);
    options.Schema.For<Auftrag>().Identity(x => x.Id);
    options.Schema.For<Baumart>().Identity(x => x.Id);
    options.Schema.For<Gehölzdaten>().Identity(x => x.Id);
    options.Schema.For<Koordinatentyp>(); 
    options.Schema.For<Baumkontrolle>().ForeignKey<Auftrag>(x => x.AuftragId);
    options.Schema.For<Baumpflege>().ForeignKey<Auftrag>(x => x.AuftragId);
})
.ApplyAllDatabaseChangesOnStartup();

References: https://martendb.io/schema/migrations.html#apply-all-outstanding-changes-upfront https://martendb.io/schema/#overriding-schema-name

Upvotes: 0

Related Questions