2014-04-15 54 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

相关问题