C# 从MVC 4应用程序访问数据库时出错

C# 从MVC 4应用程序访问数据库时出错,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我在Visual Studio 2013中创建了一个MVC4 web应用程序。我的应用程序使用EF5访问SQLServer2005数据库。在我本地的Windows7机器上,它工作正常。在本地,.NET的版本是4.0.30319.18408。当我将它部署到我的服务器(Windows server 2003,IIS 6)时,该应用程序可以工作,除非我浏览到访问数据库的页面。.NET的服务器版本为4.0.30319.1022 当应用程序尝试访问数据库时,我收到以下错误: System.InvalidO

我在Visual Studio 2013中创建了一个MVC4 web应用程序。我的应用程序使用EF5访问SQLServer2005数据库。在我本地的Windows7机器上,它工作正常。在本地,.NET的版本是4.0.30319.18408。当我将它部署到我的服务器(Windows server 2003,IIS 6)时,该应用程序可以工作,除非我浏览到访问数据库的页面。.NET的服务器版本为4.0.30319.1022

当应用程序尝试访问数据库时,我收到以下错误:

System.InvalidOperationException: Failed to set Database.DefaultConnectionFactory to an instance of the 'System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework' type as specified in the application configuration. See inner exception for details. ---> System.MissingMethodException: Constructor on type 'System.Data.Entity.Infrastructure.SqlConnectionFactory' not found. at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Data.Entity.Internal.AppConfig.<.ctor>b__1() --- End of inner exception stack trace --- Server stack trace: at System.Data.Entity.Internal.AppConfig.<.ctor>b__1() at System.Lazy`1.CreateValue() Exception rethrown at [0]: at System.Lazy`1.get_Value() at System.Data.Entity.Internal.LazyInternalConnection.Initialize() at System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName() at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.OrderBy[TSource,TKey](IQueryable`1 source, Expression`1 keySelector) at DPO.Controllers.CustomersController.Index() in c:\Users\halejd\Documents\Visual Studio 2013\Projects\DPO\DPO\Controllers\CustomersController.cs:line 98 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult);
Thrown in Customers Index
System.InvalidOperationException:未能将Database.DefaultConnectionFactory设置为应用程序配置中指定的“System.Data.Entity.Infrastructure.SqlConnectionFactory,EntityFramework”类型的实例。有关详细信息,请参阅内部异常。-->System.MissingMethodException:找不到类型为“System.Data.Entity.Infrastructure.SqlConnectionFactory”的构造函数。System.Activator.CreateInstance(System.Data.Entity.Internal.AppConfig.b_u1()上的类型、BindingFlags bindingAttr、Binder Binder、Object[]args、CultureInfo culture、Object[]activationAttributes)的System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr、Binder Binder Binder Binder、Object[]args、Object[]args、CultureInfo culture、Object[]ActivationAttribute、Object[]activationAttributes)---内部异常堆栈跟踪的结尾---服务器堆栈跟踪:在System.Data.Entity.Internal.AppConfig.b_u1()在System.Lazy`1.CreateValue()异常在[0]处重试:在System.Lazy`1.get_Value()在System.Data.Entity.Internal.LazyInternalConnection.Initialize()在System.Data.Entity.Internal.LazyInternalConnection.get\u ProviderName()处System.Data.Entity.Internal.LazyInternalContext.InitializeContext()处System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)处System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()处System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()在System.Linq.Queryable.OrderBy[TSource,TKey](IQueryable`1 source,Expression`1 keySelector)的System.Linq.Queryable.get_Provider()处的System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get\u Provider(),在DPO.Controllers.CustomerController.Index()处在c:\Users\halejd\Documents\Visual Studio 2013\Projects\DPO\DPO\Controllers\CustomerController.cs中:System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]parameters)的lambda_方法(闭包,ControllerBase,Object[])的第98行System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数),位于System.Web.Mvc.Async.AsyncControllerActionInvoker.c_uDisplayClass42.b_u41()在System.Web.Mvc.Async.AsyncResultRapper.c_uuDisplayClass8'1.b_uu7(IAsyncResult)在System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResult'1.End()在System.Web.Mvc.Async.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult asyncResult)在System.Web.Mvc.AsyncControllerActionInvoker.c_uDisplayClass39.b_u33()在System.Web.Mvc.Async.AsyncControllerActionInvoker.c_uuDisplayClass4f.b_u49()在System.Web.Mvc.Async.Async.AsyncControllerActionInvoker.c_uDisplayClass37.b_u36(IAsyncResult asyncResult asyncResult)在System.Web.Mvc.Async.Async.AsyncResultRappedAsyncResult'1.End()在System.Web.Mvc.AsyncControllerActionInvoker.Async.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)位于System.Web.Mvc.Async.AsyncControllerActionInvoker.c_uDisplayClass25.c_uDisplayClass2A.b_u20()位于System.Web.Mvc.Async.AsyncControllerActionInvoker.c_uDisplayClass25.b_u22(IAsyncResult asyncResult asyncResult);
加入客户索引
这是我的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>
    <!-- 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=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </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>

  <connectionStrings>
    <add name="DatabaseContext" connectionString="Data Source=sql01\ad2005; Initial Catalog=DPO; Persist Security Info=True; UID=DPO; pwd=dpopassword" providerName="System.Data.SqlClient" />
  </connectionStrings>


  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="no_reply@aaa.com">
        <network host="smtprelay.com" port="25" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <!--
    <authentication mode="Windows" />
    <authorization>
      <deny users="?" />
    </authorization>
    -->
    <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.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  <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>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <!--
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  -->
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=sql01\ad2005; Initial Catalog=DPO; Persist Security Info=True; UID=DPO; pwd=dpopassword" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <!--
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  -->
</configuration>

有什么想法吗?


 <connectionStrings>
    <add name="DatabaseContext" connectionString ="Data Source=<ServerIpAddress>;Initial Catalog=<databasename>;Integrated Security=False;Persist Security Info=False;User ID=<userID>;Password=<password>;MultipleActiveResultSets=True;Pooling=False" providerName="System.Data.SqlClient" />
  </connectionStrings>

在web.config文件中添加连接字符串。它不在那里。它在顶部附近。我尝试更改连接字符串的格式以匹配您的格式,但收到相同的错误。