我正在使用Visual Studio Express 2013 for Web(特别是版本12.0.21005.1 REL)。这是我使用VS2013的第一个项目,到目前为止我一直在使用VS2012。
我正在尝试在我的asp.net MVC应用程序中创建一个新控制器。我首先使用Entity Framework 5代码(.NET 4.5)。我希望Visual Studio为我创建模板(你知道,一个带有读/写/删除等视图的控制器,而不是自己编写代码)。
但是,每次我尝试创建控制器时,都会收到以下错误消息:
VS 2013中是否存在某种错误?我无法弄清楚这意味着什么,重启VS2013没有帮助。
以下是血淋淋的细节....实际上它非常简单,因为这是一个新项目,到目前为止编写的代码非常少。
我的模特:
namespace ProfessionalSite.Models
{
public class EntityModels
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int ID { get; set; }
public string EnrollmentName { get; set; }
public string Credits { get; set; }
}
// Create the class that inherits from DbContext
// The name of this class is also used as the connection string in web.config
public class EFDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
}
}
在我的web.config文件中,我有以下
<add name="EFDbContext"
connectionString="Data Source=JONSNA\SQLEXP2012WT;Initial Catalog=ProfessionalSiteDb; Integrated Security=True; MultipleActiveResultSets=True"
providerName="System.Data.SqlClient"/>
标签内的。
现在是时候创建一个控制器了。我在Solution Explorer中右键单击Controllers,然后选择Add a new Controller。
然后
当我点击Add I get
时
我无法弄清楚如何摆脱这个错误。我想作为一种解决方法,我可以自己输入代码,但我想知道这是一个错误还是我做错了什么。在VS2012中,这才刚刚起作用......
我会感激任何帮助或指示。感谢。
答案 0 :(得分:1)
您不需要EntityModels
课程,请参阅下文:
namespace ProfessionalSite.Models
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int ID { get; set; }
public string EnrollmentName { get; set; }
public string Credits { get; set; }
}
// Create the class that inherits from DbContext
// The name of this class is also used as the connection string in web.config
public class EFDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
}
然后,当您创建控制器时,只需为Model类选择Student
或Enrollment
。