实体框架EntityConnection元数据问题

时间:2010-07-14 17:24:45

标签: entity-framework metadata entityconnection

我们正在尝试动态构建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 个答案:

答案 0 :(得分:1)

是的,res://部分是错误的。查看Reflector中的资源名称(在程序集内部),而不是本地文件系统上的资源名称,以查看它们应该是什么。