c# mvc 5 添加 Controller 时出错

标签 c# asp.net asp.net-mvc visual-studio-2013 asp.net-mvc-5

在我的 mvc 项目中,我想使用 Entity Framework 添加一个带有 View 的 Controller 。我正在使用本教程进行操作 http://www.asp.net/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller

虽然我一直收到这个令人沮丧的错误 enter image description here

我多次重建我的项目。我尝试了一些在互联网上找到的解决方案,例如 Adding A New MVC 5 Controller with Views Using Entity Framework Scaffolding Error 或重新安装我的 nuget 包,重新启动 Visual Studio,检查我的 conecctiong 字符串(顺便说一句,这似乎没问题),添加新模型等。我不知道下一步该做什么,这就是我向您寻求帮助的原因。

我使用:

  • Windows 7 上的 Visual Studio 2013
  • .NET Framework 4.6.2

我的模型代码:

 public class UserResModel
   {
       [Display(Name = "ReservationID")]
       public int ReservationID { get; set; }

    [Display(Name = "Class")]
    public string ClassName { get; set; }

    [Display(Name = "When")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime ClassWhen { get; set; }

    [Display(Name = "Cancel reservation")]
    public bool CancelRes { get; set; }

最佳答案

您需要将“Key”属性添加到您的 ReservationID 属性。脚手架在自动生成 Controller 代码时需要一个 Key 才能工作。

[Key]
[Display(Name = "ReservationID")]
public int ReservationID { get; set; }

添加后,重建项目并再次尝试脚手架。

关于c# mvc 5 添加 Controller 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692887/

相关文章:

c# - 如何访问从 javascript 保存的 session 值?

c# - 获取继承自 Asp.Net MVC 中的 ApplicationUser 的自定义用户类列表

一页中多个表单的 asp.net mvc 客户端验证

ASP.NET 成员(member)资格基于 Web 的管理

asp.net-mvc - 怎么样? Controller 什么也不返回/当前 View

c# - C# 中是否有非唯一键排序列表泛型集合?

可分发 dll 的 C# 类包装器

c# - dbcontext.SaveChnages();插入数据库时​​抛出异常DbUpdateException,将EF5与Mysql结合使用

javascript - MVC 4 中使用 JavaScript 清除文本

JavaScript MVC 与 ASP.NET MVC(模式差异)