2014-04-15 83 views
0

我有一個連接到sqlexpress.I的c#.net 4.0應用程序想要進行更改,以便它連接到localDB。我會重複使用相同的mdf。爲實體框架設置localDB

目前的配置文件具有以下條目

<configuration> 
<configSections> 
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 

</configSections> 
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> 
</startup> 

    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 
    <parameters> 
     <parameter value="Data Source=.\EMSERVER; Integrated Security=True; MultipleActiveResultSets=True" /> 
    </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
</entityFramework> 

另外,在代碼

串的providerName = 「System.Data.SqlClient的」;

 // Initialize the connection string builder for the 
     // underlying provider. 
     SqlConnectionStringBuilder sqlBuilder = 
     new SqlConnectionStringBuilder(); 

     // Set the properties for the data source. 
     sqlBuilder.DataSource = serverName; 
     sqlBuilder.InitialCatalog = databaseName; 
     sqlBuilder.IntegratedSecurity = true; 
     sqlBuilder.Enlist = true; 
     sqlBuilder.MultipleActiveResultSets = true; 

     // Build the SqlConnection connection string. 
     string providerString = sqlBuilder.ToString(); 

     // Initialize the EntityConnectionStringBuilder. 
     EntityConnectionStringBuilder entityBuilder = 
     new EntityConnectionStringBuilder(); 

     //Set the provider name. 
     entityBuilder.Provider = providerName; 

     // Set the provider-specific connection string. 
     entityBuilder.ProviderConnectionString = providerString; 

     // Set the Metadata location. 
     entityBuilder.Metadata = @"res://*/TrendDataModel.csdl| 
     res://*/TrendDataModel.ssdl| 
     res://*/TrendDataModel.msl"; 

     return entityBuilder.ToString(); 

我們是否應該有代碼和app.config?我如何將其更改爲連接到位於c:\ temp中的test.mdf文件。或者它足夠我直接在代碼中更改連接字符串?

+0

我抄在問題在app.config也形成代碼 – user1687824

回答

0

EF提供了兩種類型的DbContext/ObjectContext構造函數方法。

其中一個採用連接字符串的名稱(來自配置文件),另一個採用包含ConnectionString的EntityConnection實例。這樣你就可以和其他人一起工作。

http://msdn.microsoft.com/en-us/data/dn456849

+0

我的問題是不同的內部的ConnectionString .. – user1687824

相關問題