Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Database First HasNoKey

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.

like image 611
s k Avatar asked Feb 06 '20 13:02

s k


People also ask

What is code first and database First in Entity Framework Core?

In the code first approach, the programmer has to write the classes with the required properties first, while in the database first approach, the programmer has to create first the database using GUI.

How do I use database first approach in Entity Framework?

Step 1 − Let's create a new console project with DatabaseFirstDemo name. Step 2 − To create the model, first right-click on your console project in solution explorer and select Add → New Items… Step 3 − Select ADO.NET Entity Data Model from middle pane and enter name DatabaseFirstModel in the Name field.

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

Can we use database first approach in asp net core?

NET Core. Entity Framework Core supports Database-First approach via the Scaffold-DbContext command of Package Manager Console. This command scaffolds a DbContext and entity type classes for a specified database.


1 Answers

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

like image 64
Ricardo Peres Avatar answered Oct 01 '22 16:10

Ricardo Peres