C# VS 2013 脚手架中的“运行选定的代码生成器时出错”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19920837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"There was an error running the selected code generator" in VS 2013 scaffolding
提问by Brian Webb
I'm creating a new view off of a model.
The error message I am getting is
我正在创建一个模型的新视图。
我收到的错误消息是
Error
There was an error running the selected code generator:
'Access to the path
'C:\Users\XXXXXXX\AppData\Local\Temp\SOMEGUID\EntityFramework.dll' is denied'.
错误
运行所选代码生成器时出错:
“
拒绝访问路径'C:\Users\XXXXXXX\AppData\Local\Temp\SOMEGUID\EntityFramework.dll'”。
I am running VS 2013 as administrator.
我以管理员身份运行 VS 2013。
I looked at Is MvcScaffolding compatible with VS 2013 RC by command line?but this didn't seem to resolve the issue.
我通过命令行查看了MvcScaffolding 是否与 VS 2013 RC 兼容?但这似乎并没有解决问题。
VS2013 C#5 MVC5 Brand new project started in VS 2013.
VS2013 C#5 MVC5 全新项目于 VS 2013 启动。
采纳答案by Brian Webb
Problem was with a corrupted web.config and package directory.
问题在于损坏的 web.config 和包目录。
I created the new project, and copied my code files over to the new working project, I later went back and ran diffs on the config files and a folder diff on the project itself.
我创建了新项目,并将我的代码文件复制到新的工作项目中,后来我返回并在配置文件上运行差异,并在项目本身上运行一个文件夹差异。
The problem was that the updates had highly junked up my config file with lots of update artifacts that I ended up clearing out.
问题是这些更新严重破坏了我的配置文件,其中包含许多我最终清除的更新工件。
The second problem was that the old project also kept hanging onto older DLLs that were supposed to be wiped with the application of the Nuget package. So I wiped the obj and bin folders, then the package folder. After that was done, I was able to get the older project repaired and building cleanly.
第二个问题是旧项目也一直挂在旧的 DLL 上,这些 DLL 应该被 Nuget 包的应用程序擦除。所以我擦了 obj 和 bin 文件夹,然后是包文件夹。完成后,我能够修复旧项目并干净地构建。
I have not looked into why the config file or the package folder was so borked, but I'm assuming it is one of two things.
我还没有研究为什么配置文件或包文件夹如此无聊,但我假设它是两件事之一。
- Possibly the nuget package has a flaw
- The TFS source control blocked nuget from properly updating the various dependencies.
- 可能nuget包有缺陷
- TFS 源代码控制阻止了 nuget 正确更新各种依赖项。
Since then, before applying any updates, I check out everything. However, since I have not updated EF in a while, I no evidence that this has resolved my EF or scaffolding issue.
从那时起,在应用任何更新之前,我会检查所有内容。但是,由于我有一段时间没有更新 EF,我没有证据表明这已经解决了我的 EF 或脚手架问题。
回答by Lindsey
I have seen this error with a new MVC5 project when referencing a model from a different project. Checking the path, EntityFramework.dll did exist. It was read-only though. Process monitor showed that there was an error attempting to delete the file. Setting the EntityFramework.dll in my packages folder (copy stored in source control) to writeable got around this error but brought up another one saying that it couldn't load the EntityFramework assembly because it didn't match the one referenced. My model class was defined in a different project that was using an older version of the entity framework. The MVC5 project was referencing EF 6 while the model was from a project references EF 4.4. Upgrading to EF 6 in the model's project fixed it for me.
在引用来自不同项目的模型时,我在新的 MVC5 项目中看到了此错误。检查路径,EntityFramework.dll 确实存在。虽然它是只读的。进程监视器显示尝试删除文件时出错。将我的包文件夹(存储在源代码管理中的副本)中的 EntityFramework.dll 设置为可写解决了此错误,但又提出了另一个错误,说它无法加载 EntityFramework 程序集,因为它与引用的程序集不匹配。我的模型类是在使用旧版本实体框架的不同项目中定义的。MVC5 项目引用 EF 6,而模型来自引用 EF 4.4 的项目。在模型的项目中升级到 EF 6 为我修复了它。
回答by Adam Marshall
In case it helps anyone, I renamed the namespace that the model resided in, then rebuilt the project, then renamed it back again, and rebuilt, and then it worked.
万一它对任何人有帮助,我重命名了模型所在的命名空间,然后重新构建了项目,然后再次将其重命名,然后重新构建,然后它就可以工作了。
回答by Gerrie Pretorius
What worked for me to resolve this: Close Solution, And open the project by clicking project file and not the solution file, add your controller, and bobs your uncle
解决此问题的方法是什么:关闭解决方案,然后通过单击项目文件而不是解决方案文件打开项目,添加您的控制器,然后打开您的叔叔
回答by isingh
I was able to resolve this issue and have a little better understanding of what was going on. The best part is that I am able to recreate the issue and fix it to be sure of my explanation here. The resolution was to install exactly same version of Entity Framework for both Data Access Layer project and the Web Project.
我能够解决这个问题,并对发生的事情有了更好的了解。最好的部分是我能够重新创建问题并修复它以确保我在这里的解释。 解决方案是为数据访问层项目和 Web 项目安装完全相同版本的实体框架。
My data access layer had Entity Framework v6.0.2 installed using NuGet, the web project did not have Entity Framework installed. When trying to create a Web API Controller with Entity Framework template Entity Framework gets installed automatically but its one of the older version 6.0.0. I was surprised to see two version of Entity Framework installed, newer on my Data Layer project and older on my Web Project. Once, I removed the older version and installed the newer version on Web Project the problem went away.
我的数据访问层使用 NuGet 安装了 Entity Framework v6.0.2,web 项目没有安装 Entity Framework。尝试使用实体框架模板创建 Web API 控制器时,实体框架会自动安装,但它是旧版本 6.0.0 之一。我很惊讶地看到安装了两个版本的 Entity Framework,在我的数据层项目上较新,在我的 Web 项目上较旧。有一次,我删除了旧版本并在 Web Project 上安装了新版本,问题就消失了。
回答by RitchieD
I checked all my projects and each had the same version of Entity Framework. In my case, the problem was that one of my projects was targeting .Net 4.0 while the rest were .Net 4.5.
我检查了我所有的项目,每个项目都有相同版本的实体框架。就我而言,问题是我的一个项目针对 .Net 4.0,而其余项目是 .Net 4.5。
Solution:
解决方案:
- For each project in solution Project->Properties->Application: Set Target Framework to .Net 4.5 (or whatever you need).
- Tools->Manage NuGet Package for Solution. Find Installed “Entity Framework”. And click Manage. Uncheck all projects (note the projects that require EF). Now, Re-Manage EF and check that projects that you need.
- Clean and Rebuild Solution.
- 对于解决方案 Project->Properties->Application 中的每个项目:将目标框架设置为 .Net 4.5(或任何您需要的)。
- 工具->管理解决方案的 NuGet 包。找到已安装的“实体框架”。然后单击管理。取消选中所有项目(注意需要 EF 的项目)。现在,重新管理 EF 并检查您需要的项目。
- 清洁和重建解决方案。
回答by Moslem Ben Dhaou
This is typically caused by an invalid Web.config
file. I had the same problem and it turned out I inadvertently changed the HTML comment block <!-- -->
to a server side comment block @* *@
(through a Replace All action).
这通常是由无效Web.config
文件引起的。我遇到了同样的问题,结果我无意中将 HTML 注释块<!-- -->
更改为服务器端注释块@* *@
(通过“全部替换”操作)。
And in case you are developing a WinForms application, try to look to App.config
.
如果您正在开发 WinForms 应用程序,请尝试查看App.config
.
回答by Jim Taliadoros
None of the above helped for me.
以上都对我没有帮助。
I found that the cause of my problem was overriding OnModelCreating in my context class that the scaffold item was dependent on. By commenting out this method, then the scaffolding works.
我发现我的问题的原因是覆盖了脚手架项目所依赖的上下文类中的 OnModelCreating。通过注释掉这个方法,脚手架就可以工作了。
I do wish Microsoft would release less buggy code.
我确实希望微软发布更少的错误代码。
回答by Bill B
VS2013 Error: There was an error running the selected code generator: ' A configuration for type 'SolutionName.Model.SalesOrder' has already been added ...'
VS2013 错误:运行选定的代码生成器时出错:'已添加类型'SolutionName.Model.SalesOrder'的配置......'
I had this problem while working through a Pluralsight Course "Parent-Child Data with EF, MVC, Knockout, Ajax, and Validation". I was trying to add a New Scaffolded Itemusing the template MVC 5 Controller with views, using Entity Framework.
我在学习 Pluralsight 课程“使用 EF、MVC、Knockout、Ajax 和验证的父子数据”时遇到了这个问题。我试图使用带有视图的模板MVC 5 Controller添加一个新的脚手架项,使用 Entity Framework。
The Data Context class I was using including an override of the OnModelCreating method. The override was required to add some explicit database column configurations where the EF defaults were not adequate. This override was simple, worked and no bugs, but (as noted above) it did interfere with the Controller scaffolding code generation.
我使用的数据上下文类包括对 OnModelCreating 方法的覆盖。需要覆盖来添加一些显式数据库列配置,其中 EF 默认值不足。这个覆盖很简单,有效并且没有错误,但是(如上所述)它确实干扰了控制器脚手架代码的生成。
Solution that worked for me:
对我有用的解决方案:
1 - I removed (commented out) my OnModelCreating override and the scaffolding template completed with no error messages - my controller code was generated as expected.
1 - 我删除(注释掉)我的 OnModelCreating 覆盖和脚手架模板,没有错误消息 - 我的控制器代码按预期生成。
2 - However, trying to build the project choked because 'The model had changed'. Since my controller code was was now properly generated, I restored (un-commented) the OnModelCreating override and the project built and ran successfully.
2 - 但是,由于“模型已更改”,尝试构建项目时受阻。由于我的控制器代码现在已正确生成,因此我恢复了(取消注释)OnModelCreating 覆盖并且项目构建并成功运行。
回答by simon9k
Rebuild the solution works for me. before rebuild, I find references number of my 'ApplicationDbContext' is zero, that is impossible, so rebuild solution, everything is OK now.
重建解决方案对我有用。在重建之前,我发现我的“ApplicationDbContext”的引用数为零,这是不可能的,所以重建解决方案,现在一切正常。