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
Author by
s k
Updated on February 16, 2021Comments
-
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.
-
Ramil Aliyev about 4 yearsYou can check this article docs.microsoft.com/en-us/ef/core/modeling/keyless-entity-types
-
-
s k about 4 yearsNo code first approach. The Database was created 20 years ago.
-
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 over 3 yearsI can't resolve .HasNoKey() on my newly created entity.
-
Ricardo Peres over 3 years