添加控制器时c#mvc 5错误

时间:2022-12-25 15:53:25

In my mvc project I want to add a controller with views, using Entity Framework. I'm doing it with this tutorial http://www.asp.net/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller

在我的mvc项目中,我想使用Entity Framework添加一个带视图的控制器。我正在使用这个教程http://www.asp.net/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller

Altough I keep getting this frustrating error 添加控制器时c#mvc 5错误

尽管我仍然遇到这个令人沮丧的错误

I rebuilded my project many, many times. I tried some solutions I found in the Internet like Adding A New MVC 5 Controller with Views Using Entity Framework Scaffolding Error or reinstalling my nuget package, restarting Visual Studio, checking my conecctiong string (which seems alright btw), adding new model, etc. I have no idea what to do next, that's why I'm asking You for help.

我很多次重建了我的项目。我尝试了一些我在互联网上找到的解决方案,比如使用实体框架脚手架错误添加新的MVC 5控制器或重新安装我的nuget包,重新启动Visual Studio,检查我的conecctiong字符串(看起来好吧顺便提一下),添加新模型等。我不知道接下来该做什么,这就是我向你寻求帮助的原因。

I use:

  • Visual Studio 2013 on Windows 7
  • Windows 7上的Visual Studio 2013

  • .NET Framework 4.6.2
  • .NET Framework 4.6.2

My model code:

我的型号代码:

 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; }

}

1 个解决方案

#1


0  

You need to add the 'Key' attribute to your ReservationID property. Scaffolding requires a Key to work when it auto generates the controller code.

您需要将“Key”属性添加到R​​eservationID属性中。脚手架需要Key才能在自动生成控制器代码时工作。

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

Once you add that, rebuild the project and try the scaffolding again.

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

#1


0  

You need to add the 'Key' attribute to your ReservationID property. Scaffolding requires a Key to work when it auto generates the controller code.

您需要将“Key”属性添加到R​​eservationID属性中。脚手架需要Key才能在自动生成控制器代码时工作。

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

Once you add that, rebuild the project and try the scaffolding again.

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