Beispiel #1
0
        /// <summary>
        /// 清除所有日志信息
        /// </summary>
        /// <returns>是否清除成功</returns>
        public bool ClearLogs()
        {
            bool isSuccess = false;

            try
            {
                using (System.Transactions.TransactionScope transactionScope = new System.Transactions.TransactionScope())
                {
                    using (MedicalBaseEntities context = new MedicalBaseEntities())
                    {
                        foreach (var item in context.Log_Login)
                        {
                            context.DeleteObject(item);
                        }
                        context.SaveChanges();
                    }
                    transactionScope.Complete();
                }
                isSuccess = true;
            }
            catch
            {
                isSuccess = false;
            }
            return(isSuccess);
        }
Beispiel #2
0
        /// <summary>
        /// 删除医院
        /// </summary>
        /// <param name="id">医院ID</param>
        /// <returns>是否删除成功</returns>
        public static bool DeleteHospital(int id)
        {
            bool isSuccess = false;

            using (MedicalBaseEntities context = new MedicalBaseEntities())
            {
                try
                {
                    List <Hospital> hospitalList = new List <Hospital>();
                    hospitalList = (from p in context.Hospitals
                                    where p.ID == id
                                    select p).ToList <Hospital>();

                    if (hospitalList.Count > 0)
                    {
                        context.DeleteObject(hospitalList[0]);
                        context.SaveChanges();
                        isSuccess = true;
                    }
                }
                catch
                {
                    isSuccess = false;
                }
            }

            return(isSuccess);
        }
Beispiel #3
0
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="ID">用户ID</param>
        /// <returns>是否删除成功</returns>
        public static bool DeleteUser(int ID)
        {
            bool isSuccess = false;

            using (MedicalBaseEntities context = new MedicalBaseEntities())
            {
                try
                {
                    List <User> userList = new List <User>();
                    userList = (from p in context.Users
                                where p.ID == ID
                                select p).ToList <User>();

                    if (userList != null && userList.Count > 0)
                    {
                        context.DeleteObject(userList[0]);
                        context.SaveChanges();
                        // context.CascadingDeleteOnSubmit(user);
                    }
                    isSuccess = true;
                }
                catch
                {
                    isSuccess = false;
                }
            }
            return(isSuccess);
        }
Beispiel #4
0
        /// <summary>
        /// 批量修改模板
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="context">元数据</param>
        /// <param name="TEntity">具体类型</param>
        public static void CascadingDeleteOnSubmit <T>(this MedicalBaseEntities context, T TEntity) where T : class
        {
            if (context.Connection.State == ConnectionState.Closed)
            {
                context.Connection.Open();
            }

            var entityType            = TEntity.GetType();
            var entityProperties      = TEntity.GetType().GetProperties();
            var associationProperties = entityProperties.Where(
                c => c.PropertyType.BaseType.Name == "RelatedEnd");

            if (associationProperties.Count() != 0)
            {
                foreach (var _asso in associationProperties)
                {
                    var           _items     = _asso.GetValue(TEntity, null);
                    IEnumerable   enumerable = (IEnumerable)_items;
                    List <object> list       = new List <object>();
                    foreach (var o in enumerable)
                    {
                        list.Add(o);
                    }
                    for (int i = 0; i < list.Count; i++)
                    {
                        object o = list[i];
                        CascadingDeleteOnSubmit(context, o);
                    }
                }
            }
            try
            {
                var entityKeyObject     = TEntity.GetType().GetProperty("EntityKey").GetValue(TEntity, null);
                var entityContainerName = entityKeyObject.GetType().GetProperty("EntityContainerName").GetValue(entityKeyObject, null);
                var entityKeyName       = entityKeyObject.GetType().GetProperty("EntitySetName").GetValue(entityKeyObject, null);
                System.Data.EntityKeyMember[] KeyMembers = (System.Data.EntityKeyMember[])entityKeyObject.GetType().GetProperty("EntityKeyValues").GetValue(entityKeyObject, null);

                IEnumerable <KeyValuePair <string, object> > entityKeyValues = new KeyValuePair <string, object>[] { new KeyValuePair <string, object>(KeyMembers[0].Key, KeyMembers[0].Value) };
                string    EntityKeyName = entityContainerName + "." + entityKeyName;
                EntityKey key           = new EntityKey(EntityKeyName, entityKeyValues);
                var       forDelete     = context.GetObjectByKey(key);
                context.DeleteObject(forDelete);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }