/// <summary>
/// DB First 获取连接字符串
/// </summary>
public ClientEntities()
: base("name=ClientEntities")
{
base.Database.Connection.ConnectionString = AppConfig.DataBaseConnectionString();
}
/// <summary>
/// Code First定义
/// </summary>
/// <param name="con"></param>
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();
/// <summary>
/// 获取entity模型数据库连接
/// </summary>
/// <returns></returns>
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");
}
加密解密的就自己发挥啦,网上很多很多!!!