EF Core Database First HasNoKey

.net-core entity-framework-core sql-server

Question

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.

1
0
2/6/2020 1:53:14 PM

Popular Answer

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.

1
2/6/2020 1:58:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow