动态地进行实体连接

时间:2012-03-16 16:53:56

标签: c# entity-framework

我正在使用这样的代码,但我得到一个例外:“基础提供程序在打开时失败了。”可能的原因是什么?我已在app.config文件中注释了连接字符串。

SqlConnectionStringBuilder sqlConnection = new SqlConnectionStringBuilder();

sqlConnection.DataSource = dataSource;
sqlConnection.InitialCatalog = initialCatalog;
sqlConnection.IntegratedSecurity = true;

EntityConnectionStringBuilder entityConnection = new EntityConnectionStringBuilder();
entityConnection.Provider = "System.Data.SqlClient";
entityConnection.Metadata = "res://*/Restaurant.csdl|res://*/Restaurant.ssdl|res://*/Restaurant.msl";
entityConnection.ProviderConnectionString = sqlConnection.ToString();

using (EntityConnection conn = new EntityConnection(entityConnection.ToString()))
{
    conn.Open();
    MessageBox.Show("Just testing the connection.");
    conn.Close();
}

0 个答案:

没有答案