0

I have a table defined to have exactly one row, by using a boolean primary key and a unique constraint. The idea behind this was based on this post from ten years ago: One-Row by Brandstetter. This is intentional and works fine.

But EF Core Validation generates the following warning, which I would like to suppress.

warn: Microsoft.EntityFrameworkCore.Model.Validation[20600] Property 'Id' on entity type 'Config' is part of a primary or alternate key, but has a constant default value set. Constant default values are not useful for primary or alternate keys since these properties must always have non-null unique values.

How can I suppress this warning?

My table def in Postgres:

CREATE TABLE config (
    id boolean PRIMARY KEY DEFAULT TRUE,
    demo boolean NOT NULL DEFAULT FALSE,
    version text NOT NULL,
    venue text,
    CONSTRAINT config_unique CHECK (id)
);

Relevant section of my DbContext.cs:

modelBuilder.Entity<Config>(entity =>
{
    entity.HasKey(e => e.Id).HasName("config_pkey");

    entity.Property(e => e.Id).HasDefaultValue(true);
    entity.Property(e => e.Demo).HasDefaultValue(false);
});

1 Answer 1

0

Removing the default from the boolean primary key fixed the warning.

id boolean PRIMARY KEY DEFAULT TRUE,

And apparently the missing default does not change the one-row nature of the table, since the constraint is still enforced.

Sign up to request clarification or add additional context in comments.

2 Comments

Technically though, can it not have two rows? True and False.
That's what I would have expected, so I tried it, and apparently the constraint prevents a row with Id of False from being created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.