0

I have a .net core 3.1 MVC app with EF 6. I am having it create CRUD pages using the "New scaffolded item" feature based on models that were already generated (I'm doing "database first"). The cshtml files it generates never have the primary key columns included, like there's no textboxes and labels made for them. These are not identity columns. Although for the tables that do have identity cols, it would be nice if it included it on the CRUD pages besides Create.

Model:

namespace ABCDashboard.Models
{
    public partial class TableMap
    {           
        public string EventOID { get; set; }           
        public string FormOID { get; set; }            
        public string ItemGroupOID { get; set; }
        public string TableSchema { get; set; }
        public string TableName { get; set; }
        public int? FormOrder { get; set; }
        public int? ItemGroupOrder { get; set; }
    }
}

The .cshtml files for create, details, delete, edit and index leave out EventOID, FormOID, and ItemGroupOID.

Kelly
  • 945
  • 2
  • 18
  • 31

1 Answers1

0

The solution was to add DatabaseGenerated annotations:

using System.ComponentModel.DataAnnotations.Schema;

namespace ABCDashboard.Models
{
    public partial class TableMap
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public string StudyEventOID { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public string FormOID { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public string ItemGroupOID { get; set; }
        public string TableSchema { get; set; }
        public string TableName { get; set; }
        public int? FormOrder { get; set; }
        public int? ItemGroupOrder { get; set; }
    }
}

This worked on Create, Index and Edit pages for my tables with non-identity PKs. Not sure yet how to have it put the IDs that are identity columns on the pages as well (besides Create) but that's always just one column so it's easier to add that manually.

Kelly
  • 945
  • 2
  • 18
  • 31