I'm trying to create a Razor List View using the model and the context of an SQL View I succesfully scaffolded before as it was a Table. When trying to scaffold the controller I get the 'Primary key not found' error message.
Model:
using System;
namespace PSWebase2.Models
{
public partial class ViewCalibraRmm
{
public string CódigoDoRmm { get; set; }
public DateTime? DataUc { get; set; }
public DateTime? DataPc { get; set; }
}
}
Context already has the "HasNoKey" in its properties:
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity < ViewCalibraRmm > (entity => {
entity.HasNoKey();
entity.ToView("View_Calibra_RMM");
entity.Property(e => e.CódigoDoRmm)
.HasColumnName("Código_do_RMM")
.HasMaxLength(66);
entity.Property(e => e.DataPc)
.HasColumnName("DataPC")
.HasColumnType("datetime");
entity.Property(e => e.DataUc)
.HasColumnName("DAtaUC")
.HasColumnType("datetime");
});
Scaffold the controller need primary key, because Entity Framework needs to know the key to keep track on the object when you make an update or delete operation.
Use the scaffolding tool to produce Create, Read, Update, and Delete (CRUD) pages for the movie model.
refer to:document
But Keyless entity types
Are never tracked for changes in the DbContext and therefore are never inserted, updated or deleted on the database.
refer to document
So,when trying to scaffold the controller , get the 'Primary key not found' error message.
You can use the EF Core Migrations feature to create the database. Migrations is a set of tools that create and update a database to match the data model.
Add-Migration InitialCreate
Update-Database
result: