主键列默认不可见

primary key column not visible by default

我正在学习 ASP.NET 并尝试使用 MVC 使用代码优先方法创建两个 table。以下是课程的模型 .cs 文件。

 public class Instructor
    {
        [Key]
        public string name { get; set; }
        public string address { get; set; }
        public string address_ { get; set; }
        public string email { get; set; }
    }
    public class Course
    {
        [Key]
        [DisplayName("Course")]
        [Required(ErrorMessage = "CSExxx")]
        public string progId { get; set; }
        public string subject { get; set; }
        public string semester { get; set; }
        public string description { get; set; }
        [ForeignKey("instructor")]
        public virtual string name { get; set; }
        public virtual Instructor instructor { get; set; }
    }

我添加了相应的控制器,正如预期的那样,visual studio 自动生成了视图。一旦我 运行 项目并单击下面的讲师菜单,就会打开 window。

显然,我无法看到我声明为 PK 的列 name。现在,当我单击 Create New 时,我可以看到四列,包括主键列,即 name

我的问题是为什么我在主显示中看不到主键列。

此处使用的模板和代码生成器采用技术密钥,即对用户不可见或无意义的密钥。

你选择的name值得商榷,这里简单的解决方法是引入

[Key]
public int Id { get; set; }

在你所有的 类 中。如果您确实想保留名称,只需编辑生成的 cshtml。