/// <summary>
 /// 使用MySqlDbContext执行通用任务,并返回一个值
 /// </summary>
 /// <param name="func"></param>
 public static T Func <T>(Func <MySqlDbContext, T> func)
 {
     using (MySqlDbContext context = new MySqlDbContext())
     {
         return(func(context));
     }
 }
 /// <summary>
 /// 使用MySqlDbContext执行通用任务
 /// </summary>
 /// <param name="action"></param>
 public static void Action(Action <MySqlDbContext> action)
 {
     using (MySqlDbContext context = new MySqlDbContext())
     {
         action(context);
     }
 }
        /// <summary>
        /// 安全的使用MySqlDbContext执行通用任务,并返回一个值
        /// </summary>
        /// <param name="func"></param>
        /// <param name="exceptionHanlder">异常处理</param>
        public static T FuncSafe <T>(Func <MySqlDbContext, T> func, Action <Exception> exceptionHanlder = null)
        {
            try
            {
                using (MySqlDbContext context = new MySqlDbContext())
                {
                    return(func(context));
                }
            }
            catch (Exception ex)
            {
                if (exceptionHanlder != null)
                {
                    exceptionHanlder(ex);
                }

                return(default(T));
            }
        }
        /// <summary>
        /// 该代码正常运行
        ///
        /// 默认ef的provider是sqlserver,通过注释[DbConfigurationType(typeof(MySqlEFConfiguration))]来改变
        /// </summary>
        public static void TestDynamicConnectionString()
        {
            string conStr = @"Server=127.0.0.1;Port=3306;Database=test{0}db;Uid=root;Pwd=root;Pooling=True;Max Pool Size=15;CharSet=utf8;Keepalive=16;";


            for (int i = 0; i < 8; i++)
            {
                string conStrTemp = string.Format(conStr, i);

                using (MySqlDbContext context = new MySqlDbContext(conStrTemp))
                {
                    context.ListItems.Add(new Common.Utils.sqls.EF2.CommonDbModels.ListItem()
                    {
                        Id         = GuidUtils.GetGuid32(),
                        Item       = i.ToString(),
                        CreateTime = DateTime.Now
                    });
                    context.SaveChanges();
                }
            }
        }
        /// <summary>
        /// 安全的使用MySqlDbContext执行通用任务
        /// </summary>
        /// <param name="action">数据库操作</param>
        /// <param name="exceptionHanlder">异常处理</param>
        public static bool ActionSafe(Action <MySqlDbContext> action, Action <Exception> exceptionHanlder = null)
        {
            try
            {
                using (MySqlDbContext context = new MySqlDbContext())
                {
                    action(context);
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (exceptionHanlder != null)
                {
                    exceptionHanlder(ex);
                }

                return(false);
            }
        }