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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With