Пример #1
0
        private void CheckedIdentityVlaue()
        {
            var key        = CoreUtils.GetKeyFiledName(((ExcutParBag_Insert)excutParBag).data);
            var value      = CoreUtils.GetObjectPropertyValue(((ExcutParBag_Insert)excutParBag).data, key);
            var isIdentity = CoreUtils.CheckedIsIdentity(((ExcutParBag_Insert)excutParBag).data.GetType(), key);

            if ((isIdentity && value.ToSafeString("0") != "0"))
            {
                throw new Exception("违反主键自增长约束: Insert 语句具有主键自增长的ID,在插入的时候不能具有自增长值.");
            }
        }
        /// <summary>
        /// 获取两个对象间的值发生变化的描述
        /// </summary>
        /// <typeparam name="T">T</typeparam>
        /// <param name="obj1">变化前的对象</param>
        /// <param name="obj2">变化后的对象</param>
        /// <param name="isDes">是否过滤掉没有[Description]标记的</param>
        /// <returns>字符串</returns>
        internal static List <ChangedInfo> GetChangeInfos <T>(T obj1, T obj2, HashSet <string> propertyChangedLis) where T : new()
        {
            string             res = string.Empty;
            List <ChangedInfo> lis = new List <ChangedInfo>();

            if (obj1 == null || obj2 == null)
            {
                return(new List <ChangedInfo>());
            }
            if (propertyChangedLis == null || propertyChangedLis.Count() <= 0)
            {
                return(new List <ChangedInfo>());
            }
            var properties =
                from property in (typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
                select property;

            //string objVal1 = string.Empty;
            //string objVal2 = string.Empty;


            foreach (var propertyName in propertyChangedLis)
            {
                var property = properties.Where(p => p.Name == propertyName).FirstOrDefault();


                var    objVal1      = property.GetValue(obj1, null) == null ? string.Empty : property.GetValue(obj1, null);
                var    objVal2      = property.GetValue(obj2, null) == null ? string.Empty : property.GetValue(obj2, null);
                var    keyFiledName = CoreUtils.GetKeyFiledName(typeof(T));
                var    tableName    = CoreUtils.GetTableName(typeof(T));
                var    keyVlaue     = CoreUtils.GetObjectPropertyValue(obj1, keyFiledName).ToSafeString("");
                string des          = string.Empty;
                //DescriptionAttribute descriptionAttribute = ((DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute)));
                //if (descriptionAttribute != null)
                //{
                //    des = ((DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute))).Description;// 属性值
                //}
                //if (isDes && descriptionAttribute == null)
                //{
                //    continue;
                //}
                if (objVal1 == null && objVal2 == null)
                {
                    continue;
                }
                if ((objVal1 == null && objVal2 != null) || (objVal1 != null && objVal2 == null))
                {
                    var      dispalyname = "";
                    object[] attrs       = property.GetCustomAttributes(true);

                    var f = (from p in attrs where p.GetType().Name == "DisplayAttribute" select p).FirstOrDefault();
                    if (f != null)
                    {
                        DisplayAttribute displayAttr = f as System.ComponentModel.DataAnnotations.DisplayAttribute;
                        dispalyname = displayAttr.Name;
                    }
                    lis.Add(new ChangedInfo()
                    {
                        FFiledName = property.Name, FNewValue = objVal2.ToSafeString(), FOldValue = objVal1.ToSafeString(), FOrgType = property.GetType().ToString(), FFiledDes = dispalyname, FTableName = tableName, FKeyFiledName = keyFiledName, FKeyValue = keyVlaue
                    });
                    continue;
                }

                if (!(objVal1.Equals(objVal2)))
                {
                    var      dispalyname = "";
                    object[] attrs       = property.GetCustomAttributes(true);

                    var f = (from p in attrs where p.GetType().Name == "DisplayAttribute" select p).FirstOrDefault();
                    if (f != null)
                    {
                        DisplayAttribute displayAttr = f as System.ComponentModel.DataAnnotations.DisplayAttribute;
                        dispalyname = displayAttr.Name;
                    }
                    lis.Add(new ChangedInfo()
                    {
                        FFiledName = property.Name, FNewValue = objVal2.ToSafeString(), FOldValue = objVal1.ToSafeString(), FOrgType = property.GetType().ToString(), FFiledDes = dispalyname, FTableName = tableName, FKeyFiledName = keyFiledName, FKeyValue = keyVlaue
                    });
                }
            }
            return(lis);
        }