2011-08-31 183 views
5

我在一個SQL服務器中有很多數據庫。 我將connectionString作爲模板(查看Initial Catalog={0})放入web.config中。如何創建實體框架ObjectContext?

<add name="ent" connectionString="metadata=res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=1.1.1.1;Initial Catalog={0};Persist Security Info=True;User ID=user;Password=pass;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

我想用正確的connectionString創建ObjectContext的。我想要做到以下幾點,CreatObjectContext<SiteEntities>('MySite')但我得到錯誤Unable to determine the provider name for connection of type 'System.Data.EntityClient.EntityConnection'

public T CreatObjectContext<T>(string dbName) where T : ObjectContext, new() 
{   
     var conStr = ConfigurationManager.ConnectionStrings["ent"].ConnectionString; 
     var entityBuilder = new EntityConnectionStringBuilder(conStr); 
     entityBuilder.Provider = "System.Data.SqlClient"; 
     // Build correct conString to the db 
     entityBuilder.ProviderConnectionString = string.Format(entityBuilder.ProviderConnectionString, dbName); 

     var connection = new EntityConnection(entityBuilder.ConnectionString);       
     var builder = new ContextBuilder<T>(); 

     return builder.Create(connection);   
} 

我做錯了什麼?我如何創建上下文?

回答

4

如果您使用的是EntityConnectionStringBuilder,則只需要將sqlserver連接字符串存儲在web.config中。 EntityConnectionStringBuilder可以將它們轉換爲EF4連接字符串。

例的web.config

<connectionStrings> 
     <add name="db1" connectionString="data source=localhost\SQLEXPRESS;initial catalog=db1;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" /> 
     <add name="db2" connectionString="data source=localhost\SQLEXPRESS;initial catalog=db2;integrated security=True;multipleactiveresultsets=True;App=EntityFramework" /> 
    </connectionStrings> 

而且我們可以改變你的方法是這樣的:

public ObjectContext CreatObjectContext(string dbName) 
{   
     var conStr = ConfigurationManager.ConnectionStrings[dbName].ConnectionString; 
     var entityBuilder = new EntityConnectionStringBuilder(); 

     entityBuilder.Provider = "System.Data.SqlClient"; 
     entityBuilder.ProviderConnectionString = conStr; 

     entityBuilder.MetaData = @"res://*/ent.csdl|res://*/ent.ssdl|res://*/ent.msl"; 

     return new ObjectContext(entityBuilder.ToString());   
} 
+0

相關問題實體框架運行時連接字符串http://stackoverflow.com/問題/ 11368897 /實體框架運行時連接字符串 –

2

我只是想分享一個小的類來創建使用實體類的實體框架連接作爲SQL連接字符串的類型和entityModel元數據名稱。

public static class EFConnection<T> where T : ObjectContext 
    { 
    public static T GetDatabase(string connectionString,string entityModelMetadataName) 
    { 

     var entityBuilder = new EntityConnectionStringBuilder(); 
     entityBuilder.Provider = "System.Data.SqlClient"; 
     entityBuilder.ProviderConnectionString = connectionString; 
     entityBuilder.Metadata = @"res://*/" + entityModelMetadataName + ".csdl|res://*/" + entityModelMetadataName + ".ssdl|res://*/" + entityModelMetadataName + ".msl"; 


    var _db=(T)Activator.CreateInstance(typeof(T), new object[] { entityBuilder.ToString()}); 
    return _db; 

    } 

} 

使用例如:

var _db = EFConnection<Model1Container>.GetDatabase(Settings.General.Default.DatabaseConnectionString, "Model1"); 

我沒有使用this後也把一切融合在一起:

相關問題