在使用 EF Crud 操作的 MVC 脚手架中为什么需要输入主键

In MVC Scaffolding using EF Crud operation why required primary key for input

我创建了 DB 并使用数据实体模型进行附加,因为我有 table 名称 Employee 和主键 EMPID,它们会自动递增。然后我使用 entity framework 添加脚手架插入更新删除自动。

所以它总是显示输入 EMPId 也是主键和自增。

 public partial class ProductCategory 
{
    public int ProductId { get; set; } 
    public string ProductName { get; set; } 
    public string ProductDescription { get; set; }
    public string ProductPrice { get; set; } 
    public Nullable<int> CategoryId { get; set; }
    public virtual Category Category { get; set; } 
} 

这是我的模型 class 由 EF 自动创建 –

在您的主键上添加此属性

 [Key]
 [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
 public int ProductId { get;  set; }