C# Code First 和 DB First 加密连接字符串

        /// 
        /// DB First 获取连接字符串
        /// 
        public ClientEntities()
            : base("name=ClientEntities")
        {
           base.Database.Connection.ConnectionString = AppConfig.DataBaseConnectionString();
        }
        /// 
        /// Code First定义
        /// 
        /// 
        public ClientEntities(System.Data.Common.DbConnection con):base(con, contextOwnsConnection:false)
        {
//详见书196
        }
                //Code First用法
                //var con = new SqlConnection(AppConfig.DataBaseConnectionString());
                //dbContext = new ClientEntities(con);
                //DB First 用法
                dbContext = new ClientEntities();
        /// 
        /// 获取entity模型数据库连接
        /// 
        /// 
        public static string DataBaseConnectionString()
        {
            string DataSource = ConfigurationManager.AppSettings["Server"].ToString();
            string DataBase = ConfigurationManager.AppSettings["DataBase"].ToString();
            string UserId = ConfigurationManager.AppSettings["User"].ToString();
            string PassWord = ConfigurationManager.AppSettings["Password"].ToString();
            return string.Concat("data source=" + DataSource + ";initial catalog=" + DataBase + ";persist security info=True;user id=" + UserId + ";password=" + PassWord + ";MultipleActiveResultSets=True;App=EntityFramework");
        }

加密解密的就自己发挥啦,网上很多很多!!!

你可能感兴趣的:(C# Code First 和 DB First 加密连接字符串)