Example #1
0
        public bool UpdateTableDetailsPrimaryKey(long id, bool isPrimaryKey)
        {
            try
            {
                var entity = GetTableDetails(id);
                if (entity == null)
                {
                    throw new Exception("table details id invalid!");
                }
                string tableId = entity.TableID;

                int intnum    = 0;
                var dbContext = new DAL.BaseDAL(CTMSContext);
                using (var db = dbContext.DbEntities())
                {
                    //using (var trans = db.Database.BeginTransaction())
                    //{
                    //    try
                    //    {
                    //        entity.IsPrimaryKey = isPrimaryKey;
                    //        dbContext.Update(entity);
                    //        List<string> arrPrimaryKey = new List<string>();
                    //        var lists = dbContext.FindList<Log_TableDetails>(m => m.TableID == tableId & m.IsPrimaryKey == true);
                    //        if (isPrimaryKey)
                    //        {
                    //            var entityDetails = dbContext.Find<Log_TableDetails>(m => m.ID == id);
                    //            arrPrimaryKey.Add(entityDetails.ColumnName);
                    //        }
                    //        var listDetails = from m in lists where !isPrimaryKey ? m.ID != id : true select m;
                    //        foreach (var m in listDetails)
                    //            arrPrimaryKey.Add(m.ColumnName);
                    //        var table = dbContext.Find<Log_Table>(m => m.TableID == tableId);

                    //        string strPrimaryKey = string.Empty;
                    //        foreach (var m in arrPrimaryKey.OrderBy(i => i).ToList())
                    //            strPrimaryKey += string.IsNullOrWhiteSpace(strPrimaryKey) ? m : string.Format(",{0}", m);
                    //        table.PrimaryKey = strPrimaryKey;

                    //        dbContext.Update(table);

                    //        intnum = db.SaveChanges();
                    //        trans.Commit();
                    //    }
                    //    catch (Exception)
                    //    {
                    //        trans.Rollback();
                    //    }

                    //    return intnum > 0;
                    //}
                    db.Orm.Transaction(() =>
                    {
                        entity.IsPrimaryKey = isPrimaryKey;
                        dbContext.Update(entity);
                        List <string> arrPrimaryKey = new List <string>();
                        var lists = dbContext.FindList <Log_TableDetails>(m => m.TableID == tableId & m.IsPrimaryKey == true);
                        if (isPrimaryKey)
                        {
                            var entityDetails = dbContext.Find <Log_TableDetails>(m => m.ID == id);
                            arrPrimaryKey.Add(entityDetails.ColumnName);
                        }
                        var listDetails = from m in lists where !isPrimaryKey ? m.ID != id : true select m;
                        foreach (var m in listDetails)
                        {
                            arrPrimaryKey.Add(m.ColumnName);
                        }
                        var table = dbContext.Find <Log_Table>(m => m.TableID == tableId);

                        string strPrimaryKey = string.Empty;
                        foreach (var m in arrPrimaryKey.OrderBy(i => i).ToList())
                        {
                            strPrimaryKey += string.IsNullOrWhiteSpace(strPrimaryKey) ? m : string.Format(",{0}", m);
                        }
                        table.PrimaryKey = strPrimaryKey;

                        dbContext.Update(table);

                        intnum = db.SaveChanges();
                        //trans.Commit();
                    });
                    return(intnum > 0);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }