2010-07-14 53 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