C#C
C#16mo ago
Neophyte

✅ .NET 8, EF Core, Fluent API - Schema definition is ignored when accessing via DbSet

HI, I would appreciate some hints where I have made the mistake. I am running an ASP.NET API. I am relying on EF Core for data accessing ORM.

The db is scaffolded with Code first approach. I have created and mapped a new Entity.
protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.EnsureSchema("authentication");

    migrationBuilder.CreateTable(
        schema: "authentication",
        name: "AuthenticationAttempt",
        columns: table => new
        {
            Id = table.Column<long>(type: "bigint", nullable: false)
                .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
            StateId = table.Column<Guid>(type: "uuid", nullable: false),
            IsValid = table.Column<bool>(type: "boolean", nullable: false),
            CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
            ExpiringAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
            ConsumedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_AuthenticationAttempt", x => x.Id);
        });
}

Update-Database run properly, the schema and and table created properly.

When I try to test and persist an entity, it results in error:
var dummyAttempt = new AttemptEntity();
await _context.AuthenticationAttempt.AddAsync(dummyAttempt);
await _context.SaveChangesAsync();


Error:
ERROR:  relation "AuthenticationAttempt" does not exist at character 13
STATEMENT:  INSERT INTO "AuthenticationAttempt" ("ConsumedAt", "CreatedAt", "ExpiringAt", "IsValid", "StateId")


QQ: what am I missing making the Db operation look for the table at the default schema, instead of the specified one?
Was this page helpful?