简体   繁体   English

尝试创建ASP.NET MVC 4控制器时出现FileNotFound异常

[英]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:

编译转换代码时出现FileNotFoundException

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 as System.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.

相关问题 为什么在上传处理时我的 ASP.NET Core 7 MVC 中出现 FileNotFOund 异常? - Why am I getting a FileNotFOund Exception in my ASP.NET Core 7 MVC while upload handling? 尝试在 ASP.NET MVC 中的 UI controller 中显示异常消息 - Trying to show exception message in UI controller in ASP.NET MVC Controller中的异常处理(ASP.NET MVC) - Exception handling in Controller (ASP.NET MVC) 处理asp.net mvc控制器中的异常 - Handle exception in asp.net mvc controller 尝试在MVC 5 / ASP.NET中创建论坛 - Trying to create a Forum in MVC 5 / ASP.NET 尝试解析控制器时,Asp.Net MVC Unity依赖项注入会引发异常 - Asp.Net MVC Unity dependency injection gives an exception when trying to resolve a controller Asp.NET MVC:尝试调用删除控制器方法时出错 - Asp.NET MVC : Error while trying to call delete controller method 在我尝试创建 ASP.NET MVC 项目时,创建标识时出现脚手架项错误 - Scaffolding item error with creating the identity, while I was trying to create an ASP.NET MVC project 使用TDK在Asp.net Mvc中创建控制器时出错 - Getting error while creating controller in Asp.net Mvc with tdk 在Asp.net MVC中使用Html.Action时发生异常 - Exception while using Html.Action in Asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM