[英]FileNotFound exception while trying to create ASP.NET MVC 4 controller
I'm trying to follow the Contoso University tutorial , only with SQL Server 2008 instead of CE. 我正在尝试遵循Contoso大学教程 ,仅使用SQL Server 2008而不是CE。 I can connect to the database engine in SQL Server Management Studio using my PC's name and windows authentication.
我可以使用我的PC名称和Windows身份验证连接到SQL Server Management Studio中的数据库引擎。 I haven't created a database with it because I'm trying to use EF Code First.
我没有用它创建数据库,因为我正在尝试使用EF Code First。
This is my connection string: 这是我的连接字符串:
<add
name="SchoolContext"
connectionString="Data Source=CARSON-PC\CARSON;Integrated Security=true"
providerName="System.Data.SqlClient"/>
I try to add a controller by right-clicking the controllers folder and selecting Add->Controller...
and setting up the resulting dialog according to the tutorial. 我尝试通过右键单击controllers文件夹并选择
Add->Controller...
并根据教程设置生成的对话框来添加控制器。 I wind up with a dialog telling me a FileNotFoundException
was thrown, and the file is a temporary dll: 我结束了一个对话框,告诉我抛出了
FileNotFoundException
,该文件是一个临时的dll:
This dialog is thrown up five times (once for each of the generated template files, I assume) and I wind up with a controller but no generated templates. 这个对话框被抛出五次(我假设每个生成的模板文件一次),我最终得到一个控制器,但没有生成模板。
I suspect it's to do with either my connection string or my SQL server installation, since I've been stuck on this a while, getting various errors at this stage as I try and get the connection string right. 我怀疑它与我的连接字符串或我的SQL服务器安装有关,因为我已经坚持了一段时间,在此阶段遇到各种错误,因为我尝试获得正确的连接字符串。
For completeness, here's the model I'm trying to generate a controller for: 为了完整性,这是我正在尝试生成控制器的模型:
public class Student {
public int StudentID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
And the context: 背景情况:
public class SchoolContext : DbContext {
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(modelBuilder As DbModelBuilder) {
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Is my connection string wrong? 我的连接字符串错了吗? Is this a symptom of something not configured right with the server?
这是服务器未正确配置的症状吗? What am I screwing up here?
我搞砸了什么?
This turned out to be a simple bug in the MVC template files List.tt, Edit.tt, Details.tt, etc. There was an ambiguous reference to ColumnAttribute
. 事实证明,这是在MVC模板文件List.tt,Edit.tt,Details.tt等有不明确的引用一个简单的错误
ColumnAttribute
。
I opened C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\ItemTemplates\\CSharp\\Web\\MVC 4\\CodeTemplates\\AddView\\CSHTML
, and going through each of the .tt
files and searching for " ColumnAttribute" (with the space at the beginning): 我打开
C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\ItemTemplates\\CSharp\\Web\\MVC 4\\CodeTemplates\\AddView\\CSHTML
,并浏览每个.tt
文件并搜索“ColumnAttribute” (开头的空格):
//...
var column = attribute as ColumnAttribute;
if (column != null && column.IsPrimaryKey) { // LINQ to SQL
return true;
}
//...
I changed that first line to: 我将第一行更改为:
var column = attribute asSystem.Data.Linq.Mapping.ColumnAttribute;
I think only Empty.tt didn't have it. 我认为只有Empty.tt没有它。 I did the same for the VB templates.
我为VB模板做了同样的事情。 Now the controllers are created fine.
现在控制器创建得很好。
I have just tested this with ASP.NET MVC 4, SQL SERVER 2008 Express. 我刚刚使用ASP.NET MVC 4,SQL SERVER 2008 Express对此进行了测试。 Every thing works.
每件事都有效。 You need to give the database name in connection string, which is used by EF to automatically create database with this name if you have this type code in global.asax,
您需要在连接字符串中提供数据库名称,如果您在global.asax中有此类型代码,则EF使用该字符串自动创建具有此名称的数据库,
System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<SchoolContext>());
Here is my connection string, 这是我的连接字符串,
<add name="SchoolContext" connectionString="Data Source=.\SQlEXPRESS;Initial Catalog=SchoolDatabase;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />
Make sure, 确保,
Build the application before use. 使用前构建应用程序。
Try to run the application as an administrator. 尝试以管理员身份运行该应用程序。
You don't need to create SchoolContext initially, the wizard will automatically a one for you. 您最初不需要创建SchoolContext,向导会自动为您创建一个。
A simple way to find the connection string is to use Server Explorer. 查找连接字符串的一种简单方法是使用服务器资源管理器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.