2012-07-15 45 views
1

我在我的C#项目中使用EF。我们即将添加其他数据库与其他连接字符串的客户端。
DB之间不需要同步。
我尝试使用以下c'tor(供给的第二总分3):实体框架 - 为构造函数提供连接字符串

public AppEntities(string connectionString) 

和我得到以下错误:“不支持关键字:‘’。”数据源 连接字符串如下:

metadata=res://EntityFramework/App.csdl|res://EntityFramework/App.ssdl|res://EntityFramework/App.msl;provider=System.Data.SqlClient;provider connection string="Data Source=dbname\dbname;Initial Catalog=App;Persist Security Info=True;User ID=User;Password=password;MultipleActiveResultSets=True" 

这是从App.Config中取出时使用相同的连接字符串。我不想把它从那里,而是构建它自己(因此留给用户名&密码硬编码在我的应用程序的代码,而不是自由文本。
任何想法?

感谢。

回答

3

connectionstring参数不是Entity Framework Connectionstring(与.config文件相同),但它必须是“常规”.net连接字符串。

链接的示例显示如何将ef连接字符串“转换”为正常连接字符串(使用EntityConnectionStringBuilder.ToString() method)。如果你传递了ap p.config没有准备好的字符串,你会得到提到的错误。

您可以使用EntityConnectionStringBuilder解析app.config的值并将其转换。

希望有所帮助。

相关问题