简体   繁体   English

无法通过 Entity/C# 中的嘲笑生成新的 controller

[英]Cannot generate a new controller via scoffolding in Entity/C#

I am taking a tutorial on getting started with Entity Framework and C#.我正在学习如何开始使用 Entity Framework 和 C#。 I was following the [tutorial][1] and I am getting an error when I tried to create a new scaffolding item.我按照 [tutorial][1] 进行操作,但在尝试创建新的脚手架项目时出现错误。 When I select the Model class, data context class and select the controller name, I get the following error message: "There was an error running the selected code generator: 'There was an error getting the type 'ContosoUniversity.Models.Student'. Try rebuilding the project." When I select the Model class, data context class and select the controller name, I get the following error message: "There was an error running the selected code generator: 'There was an error getting the type 'ContosoUniversity.Models.Student'.尝试重建项目。”

I did rebuild the project, twice actually, and I keep getting this error message.我确实重建了项目,实际上两次,我一直收到这个错误消息。 Can anybody tell me what might be happening?谁能告诉我可能会发生什么?

These are the steps I took to create the new scaffold item:这些是我创建新的脚手架项目所采取的步骤:

  • Right-click the Controllers folder in Solution Explorer, select Add, and then click New Scaffolded Item.右键单击解决方案资源管理器中的 Controllers 文件夹,select 添加,然后单击新建脚手架项目。

    In the Add Scaffold dialog box, select MVC 5 Controller with views, using Entity Framework, and then choose Add.在添加脚手架对话框中,select MVC 5 Controller 带有视图,使用实体框架,然后选择添加。 In the Add Controller dialog box, make the following selections, and then choose Add:在添加 Controller 对话框中,进行以下选择,然后选择添加:

    Model class: Student (ContosoUniversity.Models). Model class:学生(ContosoUniversity.Models)。 (If you don't see this option in the drop-down list, build the project and try again.) (如果您在下拉列表中没有看到此选项,请构建项目并重试。)

    Data context class: SchoolContext (ContosoUniversity.DAL).数据上下文 class:SchoolContext (ContosoUniversity.DAL)。

    Controller name: StudentController (not StudentsController). Controller 名称:StudentController(不是StudentController)。

    Leave the default values for the other fields.保留其他字段的默认值。

This is how I have the code set up in my Student class file, under Models:这就是我在我的学生 class 文件中的模型下设置代码的方式:

 using System; using System.Collections.Generic; namespace ContosoUniversity.Models { public class Student { public int ID { get; set; } public string LastName { get; set; } public string FirstMidName { get; set; } public DateTime EnrollmentDate { get; set; } public virtual ICollection<Enrollment> Enrollments { get; set; } } }

Finally, this is how I have my connections set up to the localDB that is being used in the tutorial.最后,这就是我设置与教程中使用的 localDB 的连接的方式。

> <connectionStrings>
>     <add name="SchoolContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial
> Catalog=ContosoUniversity1;Integrated Security=SSPI;"
> providerName="System.Data.SqlClient"/>   </connectionStrings>

Change your connection string更改连接字符串

From this由此

<add name="xxx" connectionString="Data Source=xxx;initial catalog=xxx;Persist Security Info=True;User ID=xxxx;Password=xxxx;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

To this对此

<add name="xxx" connectionString="metadata=res://*/EFMOdel.csdl|res://*/EFMOdel.ssdl|res://*/EFMOdel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=xxxx;initial catalog=xxxx;persist security info=True;user id=xxxx;password=xxx;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM