DBContext中的连接字符串

时间:2015-04-02 08:09:47

标签: c# winforms entity-framework postgresql npgsql

我想在dbcontext文件中添加连接字符串而不是在App配置文件中,因为安全性和多个databases.i m使用npgsql(postgresql数据库的.net提供程序)。 这是我的编码。

 public partial class ADBEntities : DbContext
    {
        public ADBEntities()
        {
            Npgsql.NpgsqlConnectionStringBuilder ssb = new NpgsqlConnectionStringBuilder();
            ssb.Host = "localhost";
            //ssb.Port = 5432;
            ssb.UserName = "postgres";
            ssb.Password = "a5xxx";
            ssb.Database = "adb";
            ssb.CommandTimeout = 30;
            ssb.Pooling = true;
            ssb.MaxPoolSize = 20;
            ssb.MinPoolSize = 1;
            ssb.PreloadReader = true;
            ssb.IntegratedSecurity = true;

            var cond= new NpgsqlConnection(ssb);
            this.Database.Connection.ConnectionString = cond.ConnectionString;
        }
}

但它不起作用并且给出错误"关键字"端口"不受支持。

2 个答案:

答案 0 :(得分:0)

 SqlConnection con = new SqlConnection("Data Source=TCS3\\SQLEXPRESS;Initial Catalog=551680A3622135F7C7A89A2044CE6B6E_12 2014\\20 12 14\\BACKUP 19 NEW\\19 NEW\\SCHOOLMANAGEMENT\\SCHOOLMANAGEMENT\\APP_DATA\\TCS_SCHOOL.MDF;Persist Security Info=True;User ID=sa;Password=sql2008");

答案 1 :(得分:0)

cond.ConnectionString值如何?

尝试使用string.Format()自己构建连接字符串并将其设置为this.Database.Connection.ConnectionString