2012-12-02 42 views
1

實體連接字符串在我的網站我有定期ADO.NET連接字符串,如: ,建立從ADO.NET

現在Entity Framework添加它自己的連接字符串到同一個數據庫。

如何才能在運行時只使用一個ADO.NET連接字符串並構建實體連接字符串?

+0

我相信實體連接字符串添加元數據連接字符串,因此不能確定是否可以使用ADO.NET作爲實體連接字符串 –

+0

我需要從ADO.NET獲取數據並綁定基於ADO.NET的實體連接字符串 – ihorko

回答

3

可以使用EntityConnectionStringBuilder在運行時動態在運行時生成連接字符串,但你仍然需要metada此

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://*/AdventureWorksModel.csdl| 
           res://*/AdventureWorksModel.ssdl| 
           res://*/AdventureWorksModel.msl"; 
using (EntityConnection conn = 
    new EntityConnection(entityBuilder.ToString())) 
{ 
    conn.Open(); 
    Console.WriteLine("Just testing the connection."); 
    conn.Close(); 
} 
+0

如何基於ADO.NET連接字符串建立此實體連接字符串? – ihorko

+0

連接字符串仍然只是一個連接字符串,您只需擁有Provider,ProviderConnectionString就像用戶信息等等,並且可以通過手動創建EF連接字符串並將元數據複製到您的元數據獲得元數據entityBuilder.Metadata在運行時 –