【发布时间】:2020-02-25 13:05:55
【问题描述】:
我再次提出这个问题,因为之前的问题没有解决。
我已经在 .Net 4.5 (MVC4) 上安装了 EntityFramework 5.0.0,方法是使用 linq 查询根据 id 获取员工的结果。
public ActionResult Details(int id)
{
EmployeeContext empctxt = new EmployeeContext();
Employee employee = empctxt.Employees.Single(emp => emp.employeeid == id);
return View(employee);
}
应显示输入 id 员工的记录,但在行 EmployeeContext empctxt = new EmployeeContext(); 上给出运行时错误为 The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
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=169433
-->
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<httpRuntime targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5" />
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="EmployeeContext" providerName="System.Data.SqlClient" connectionString="SERVER=DESKTOP-DO6F13P;DATABASE=mvctrialdbase;uid=sa;pwd=sa@123;"/>
</connectionStrings>
</configuration>
【问题讨论】:
-
问题实际上不在发布的代码中,而是实体框架无法解析应用程序配置文件中的配置(
Web.config)。请确保配置有效,尤其是 EF 提供程序和连接字符串。您还可以使用配置文件的内容更新问题(不含敏感数据) -
@MartinZikmund 请检查上面的问题,我已经用 Web.Config 代码更新了问题。
-
请再次运行应用程序,当抛出异常时,检查
InnerException及其内容。它应该提供更多细节。请同时使用此信息更新问题 -
先生如何检查 MVC4 中的 InnerException?实际上我是 MVC 的新手,因此不知道如何检查它。
-
当抛出异常时,你应该会看到一个带有异常的“弹出窗口”,其中应该是一个指向“异常详细信息”的链接,如果你点击它,你应该会看到一种属性浏览器,其中应该是名为“InnerException”的属性,它应该包含另一个带有一些信息的异常。你甚至可以发布截图,我可以帮你导航到那里 (social.technet.microsoft.com/wiki/cfs-file.ashx/__key/…)
标签: c# entity-framework asp.net-mvc-4 .net-4.5