实体框架的默认连接在哪里定义?

时间:2016-10-31 07:52:16

标签: c# asp.net entity-framework

我使用Entity Framework创建一个ASP.NET Web应用程序。我的web.config就像这样

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>

我想在SQL Server Management Studio中查看我的数据库。我无法找到在Entity Framework中存储我的数据的服务器的位置。直到我使用Glimpse来查看它的位置。所以我的数据库是在.\SQLEXPRESS创建的。我想将默认连接更改为另一台服务器。我尝试过这样的事情

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

但它导致了错误ArgumentException。那么我应该做些什么来更改默认连接服务器?

我使用Code First Approach创建数据库

1 个答案:

答案 0 :(得分:1)

请查看下面的web.config示例

<configuration>
   <configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
          </configSections>
          <connectionStrings>
            <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-IHIPosterPresentationApp-20160104113839;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-IHIPosterPresentationApp-20160104113839.mdf" providerName="System.Data.SqlClient" />

            <add name="IHIPosterAppEntities" connectionString="metadata=res://*/Models.IHIPosterAppDbEntityModel.csdl|res://*/Models.IHIPosterAppDbEntityModel.ssdl|res://*/Models.IHIPosterAppDbEntityModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=IHIPosterAppDev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
          </connectionStrings>
        <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      </entityFramework>
    </configuration>