Web Api Scaffolding - 运行所选代码生成器时出错

Chr*_*ter 5 c# entity-framework visual-studio asp.net-web-api

简介:在Visual Studio 2015(Update 1)中创建新的Web项目时,我无法使用与Entity Framework相关的控制器的脚手架工具.

VS2013 Update 5也出现同样的问题

它返回以下消息:

运行所选代码生成器时
出错:获取类型"failtest.Models.TestModelClass"时出错.
尝试重建项目.

我试图重建它,但仍然没有.

任何人都知道为什么我得到这个?我实际上重新安装了我的Windows 10系统,看看是否可以修复此错误. - 现在,那个说,无论如何它需要一些强硬的爱,所以我想到它是关于时间的 - 也许它崩溃了,因为我在过去的时间里尝试了大量的节点内容,以及权限错误.但是在重新安装它之前和之后都出现了错误.(完全鞭子)

这是Visual Studio 2015 Professional的全新安装 - 基本上没有其他任何内容添加到系统中.

当我第一次开始这篇文章时,我为我的截图创建了一个空白项目,只是为了显示整个过程(下图) - 我觉得我在失去理智.

起初,测试项目突然工作,所以我创建了第二个项目再次测试.然后它失败了同样的错误.所以只是为了确认,我删除了第一个有效的项目,并使用与突然工作的名称和文件位置相同的名称和文件位置再次设置(删除所有文件夹和现有项目文件后) - 现在它再次失败.

如何重现:

  1. 创建一个新的"Azure Api App"项目 - 将所有设置保留为默认值(任何项目类型实际上都会产生相同的结果)

新项目

  1. 创建一个新模型

模型

  1. 构建你的项目(这是微软的一篇文章中提到的使用脚手架的必要条件)

  2. 通过右键单击"Controllers"文件夹添加一个新的Controller,然后选择New - > Controller - 选择" 带有操作的Web Api 2.0控制器,使用Entity Framework "

添加控制器

  1. 填写信息,因为这是一个新项目,我们没有上下文 - 所以单击+图标并创建一个新的.

填写信息

  1. 产生错误

错误

编辑:

Packages.config:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
  <package id="Swashbuckle" version="5.3.1" targetFramework="net452" />
  <package id="Swashbuckle.Core" version="5.3.1" targetFramework="net452" />
  <package id="System.IdentityModel.Tokens.Jwt" version="4.0.2.206221351" targetFramework="net452" />
  <package id="WebActivatorEx" version="2.1.0" targetFramework="net452" />
</packages>
Run Code Online (Sandbox Code Playgroud)

Web.Config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301879
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings></appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>
  <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)

编辑2:我怀疑这是一个EntityFramework问题,因为脚手架适用于不包含EntityFramework的所有内容.

已经过测试

  • 在Debug和Release配置之间切换
  • 更新了所有包和插件
  • 验证了解决方案文件和项目XML
  • 已验证的Web.config
  • 删除Bin/Debug文件夹
  • 具有不同项目类型的新项目

描述相同错误的问题 - 所有测试

我还在aspnet Github问题跟踪器上创建了一个问题. https://github.com/aspnet/Tooling/issues/411

并向微软报告了它在Visual Studio中的内部反馈系统.

Chr*_*ter 0

找到解决方案/问题。

我正在使用 Panda Antivirus - 在整个过程中,我经常检查 Panda 是否报告任何被阻止的文件、线程或对系统的任何威胁。它没有报告任何内容 - 所以我排除了防病毒软件的问题。

在阅读另一篇关于不同问题的文章后,我注意到解决方案之一是停用防病毒软件并运行修复。在尝试了其他所有方法之后,我只做了这个。

它又开始工作了。最后。〜20天后。

似乎不需要修复安装,因为再次激活防病毒软件会在您下次尝试运行脚手架时产生问题。

禁用进程监视器并不能解决问题,并且将所有文件夹添加到 AV 的例外文件夹中将需要大量维护,因此我可能会关闭我的 AV。

希望这对遇到此问题的其他人有所帮助。

非常感谢@oleg试图在这里帮助我!