2010-07-14 52 views
1

我們正在嘗試動態構建一個EntityConnection,以便不同的用戶連接到在運行時確定的不同的數據庫。爲了做到這一點,我們正在測試這裏找到的代碼:http://msdn.microsoft.com/en-us/library/bb738533.aspx。我們下面實現了這個:實體框架實體連接元數據問題

' Specify the provider name, server and database. 
Dim providerName As String = "System.Data.SqlClient" 
Dim serverName As String = "OurDBServerName" 
Dim databaseName As String = "OurDBName" 

' Initialize the connection string builder for the 
' underlying provider. 
Dim sqlBuilder As New SqlConnectionStringBuilder 

' Set the properties for the data source. 
sqlBuilder.DataSource = serverName 
sqlBuilder.InitialCatalog = databaseName 
sqlBuilder.IntegratedSecurity = False 
sqlBuilder.UserID = "OurAppUserName" 
sqlBuilder.Password = "OurPassword" 

' Build the SqlConnection connection string. 
Dim providerString As String = sqlBuilder.ToString 

' Initialize the EntityConnectionStringBuilder. 
Dim entityBuilder As New EntityConnectionStringBuilder 

'Set the provider name. 
entityBuilder.Provider = providerName 

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

' Set the Metadata location to the current directory. 
entityBuilder.Metadata = "res://*/NotaModel.csdl|" & _ 
         "res://*/NotaModel.ssdl|" & _ 
         "res://*/NotaModel.msl" 

Console.WriteLine(entityBuilder.ToString) 

Using conn As EntityConnection = New EntityConnection(entityBuilder.ToString) 
    conn.Open() 
    Console.WriteLine("Just testing the connection.") 
    conn.Close() 
End Using 

當conn.Open()運行時拋出一個錯誤:「無法加載指定的元數據資源」它似乎表明一個或多個「res:// * ...」引用是錯誤的。我已經確認該項目確實包含這些文件(位於bin/debug文件夾下)。我們在這裏想念什麼 - 有什麼想法?

謝謝

回答

1

是的,res://部分是錯誤的。查看Reflector(程序集內)中的資源名稱,而不是本地文件系統中的資源名稱,以查看它們應該是什麼。

+0

這可能是正確的參考是不正確的 - 不幸的是使用反射器沒有向我透露名稱(或更好地說:我現在沒有時間來學習如何正確使用它)。但是我發現使用一個簡單的非資源引用做到了這一點:entityBuilder.Metadata =「NotaModel.csdl | NotaModel.ssdl | NotaModel.msl」而不是:entityBuilder.Metadata =「res://*/NotaModel.csdl | 「 &_「res://*/NotaModel.ssdl |」 &_「res://*/NotaModel.msl」 – Gatmando 2010-07-15 15:10:33