EF Core Database First HasNoKey

23,347

In your DbContext class' OnModelCreating method, you do something like this:

modelBuilder
    .Entity<MyEntity>(builder =>
    {
        builder.HasNoKey();
        builder.ToTable("MY_ENTITY");
    });

Read more about it here:
https://docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types?tabs=fluent-api

Share:
23,347
s k
Author by

s k

Updated on February 16, 2021

Comments

  • s k
    s k about 3 years

    I have a database table created 20 years ago in my company, it is only used for journaling, with NO PRIMARY KEY or INDEX. I can't alter that table.

    I understood that we need to call HasNoKey() in code first approach.

    But how can I apply HasNoKey in this case? Since it is a Database First approach.

  • s k
    s k about 4 years
    No code first approach. The Database was created 20 years ago.
  • War
    War almost 4 years
    @sk This should still apply to DB first ... DB first IS code first, it's just that we generate the Entity Types from the DB first, but it should still create a DbContext with a ConfigureModel method in to which we tell the model that this is going on in the DB.
  • Slagmoth
    Slagmoth over 3 years
    I can't resolve .HasNoKey() on my newly created entity.
  • Ricardo Peres
    Ricardo Peres over 3 years