不将EF连接字符串写在配置文件的方法

edmx的构造函数:

public DecorationMSEntities() : base(myConfig.DataBaseConnectionString, "DecorationMSEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

EF连接字符串模板类: 

 public class myConfig
    {
     public static string DataBaseConnectionString
       {
               return
                   EFConnectionStringModle("EFname", @"(local)\instance",
                   "yourDbName","sa","yourpassword"); }
       }

       private static string EFConnectionStringModle(string EntityName,string DBsoure,string DBname, string ID, string Password)
       {
           return string.Concat("metadata=res://*/",
               EntityName, ".csdl|res://*/",
               EntityName, ".ssdl|res://*/",
               EntityName, ".msl;provider=System.Data.SqlClient;provider connection string='Data Source=",
               DBsoure, ";Initial Catalog=",
               DBname,";Persist Security Info=True;User ID=",
               ID, ";Password=",
               Password, ";MultipleActiveResultSets=True'");
          
       }

posted @ 2013-07-30 13:48  Ruiky  阅读(3404)  评论(0编辑  收藏  举报