【问题标题】:ASP NET MVC 5 TestMethod Entity ExceptionASP NET MVC 5 TestMethod 实体异常
【发布时间】:2017-01-21 09:47:24
【问题描述】:

我的 Visual Studio 2015 有一个 MVC 5 项目。 我已经使用 ADO 实体数据模型首先创建了我的域模型数据库,并且我有一个自定义身份(我不知道它是否重要),直到这里一切正常。问题是当我开始创建我的 UnitTest 类时。首先我用鼠标右键单击并选择创建单元测试,所以我做了类似的事情

    [TestMethod()]
    public void PermissionsTest()
    {
        GroupController controller = new GroupController();
        var result = controller.Permissions("1") as ViewResult;
        Assert.AreEqual("Details", result.ViewName);
    }

我也将我的 MVC 项目引用放入我的测试项目中,并更新了我的 app.config,添加了我在 MVC 项目中使用的连接字符串。 但我收到异常:

  Result Message:   
  Test method Controllers.Tests.GroupControllerTests.PermissionsTest threw exception: 
  System.Data.Entity.Core.MetadataException: Schema specified is not valid. Errors: 
  Models.Domain.Models.ssdl(2,2) : error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

那么有人可以帮助我吗?

根据 Wikitor 的建议,我的 app.config 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=****.****.com;Initial Catalog=LDC;Integrated Security=False;User ID=****;Password=****;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient" />
    <add name="LDCEntities" connectionString="metadata=res://*/Models.Domain.LdcModels.csdl|res://*/Models.Domain.LdcModels.ssdl|res://*/Models.Domain.LdcModels.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=****.***.***;initial catalog=LDC;persist security info=True;user id=****;password=*****;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

【问题讨论】:

    标签: c# asp.net-mvc entity-framework unit-testing


    【解决方案1】:

    你忘记了这个带有 entityFramework 的配置部分 :)

    <?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=301880
          -->
        <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>
          <connectionStrings>
            <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Vegan-20150428012206.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
          </connectionStrings>
          <appSettings>
            <add key="webpages:Version" value="3.0.0.0" />
            <add key="webpages:Enabled" value="false" />
            <add key="ClientValidationEnabled" value="true" />
            <add key="UnobtrusiveJavaScriptEnabled" value="true" />
          </appSettings>
          <system.web>
            <authentication mode="None" />
            <compilation debug="true" targetFramework="4.5.2" />
            <httpRuntime targetFramework="4.5.2" />
            <httpModules>
              <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
            </httpModules>
          </system.web>
          <system.webServer>
            <modules>
              <remove name="FormsAuthentication" />
              <remove name="ApplicationInsightsWebTracking" />
              <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
            </modules>
            <validation validateIntegratedModeConfiguration="false" />
          </system.webServer>
          <runtime>
            <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
              <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
              </dependentAssembly>
              <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
              </dependentAssembly>
              <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
              </dependentAssembly>
              <dependentAssembly>
                <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
              </dependentAssembly>
              <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.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
              </dependentAssembly>
              <dependentAssembly>
                <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
              </dependentAssembly>
              <dependentAssembly>
                <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
              </dependentAssembly>
              <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
              </dependentAssembly>
              <dependentAssembly>
                <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
              </dependentAssembly>
              <dependentAssembly>
                <assemblyIdentity name="Microsoft.Practices.ServiceLocation" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
              </dependentAssembly>
            </assemblyBinding>
          </runtime>
          <entityFramework>
            <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
              <parameters>
                <parameter value="MSSQLLocalDB" />
              </parameters>
            </defaultConnectionFactory>
            <providers>
              <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            </providers>
          </entityFramework>
          <system.codedom>
            <compilers>
              <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
              <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
            </compilers>
          </system.codedom>
        </configuration>
    

    【讨论】:

    • 感谢 Wiktor,我尝试在我的项目中放置相同的标签“entityFramework”,现在我有了 System.TypeInitializationException:'System.Data.Entity.Internal.AppConfig' 的类型初始化程序抛出异常。 ---> System.Configuration.ConfigurationErrorsException:配置系统初始化失败---> System.Configuration.ConfigurationErrorsException:无法识别的配置部分entityFramework。你知道问题出在哪里吗?发布我的 app.config 会有帮助吗?我将在上面更新我的问题
    • 也许你把它放错地方了。我想帮忙,但现在我不在家。祝你好运,希望你能解决这个问题。我过去也遇到过这样的问题:(你更新会有所帮助
    • @diegocolli 在 MVC 项目中有 两个 WebConfig.cs 文件。请确保在 \Views 文件夹中 NOT 的文件夹中有此文件。
    • 我想你忘记了这一点。我认为这会有所帮助。我更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-06-18
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多