Exemplo n.º 1
0
        /// <summary>
        /// 把实体entiy更新到数据库
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual void Update(TEntity entity)
        {
            int      num;
            Database database = this.CreateDAO();

            if (entity is ISerializableProperties)
            {
                ISerializableProperties properties = entity as ISerializableProperties;
                if (properties != null)
                {
                    properties.Serialize();
                }
            }
            if ((Repository <TEntity> .RealTimeCacheHelper.PropertyNameOfBody != null) && (Repository <TEntity> .RealTimeCacheHelper.PropertyNameOfBody.GetValue(entity, null) == null))
            {
                PocoData      data = PocoData.ForType(typeof(TEntity), this.database.DefaultMapper);
                List <string> list = new List <string>();
                foreach (KeyValuePair <string, PocoColumn> pair in data.Columns)
                {
                    if (((string.Compare(pair.Key, data.TableInfo.PrimaryKey, true) != 0)) && ((string.Compare(pair.Key, Repository <TEntity> .RealTimeCacheHelper.PropertyNameOfBody.Name, true) != 0) && !pair.Value.ResultColumn))
                    {
                        list.Add(pair.Key);
                    }
                }
                num = database.Update(entity, (IEnumerable <string>)list);
            }
            else
            {
                num = database.Update(entity);
            }
            if (num > 0)
            {
                this.OnUpdated(entity);
            }
        }
Exemplo n.º 2
0
        public virtual object Insert(TEntity entity)
        {
            if (entity is ISerializableProperties)
            {
                ISerializableProperties serializableProperties = entity as ISerializableProperties;
                if (serializableProperties != null)
                {
                    serializableProperties.Serialize();
                }
            }
            object result = this.CreateDAO().Insert(entity);

            this.OnInserted(entity);
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 把实体entity添加到数据库
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public virtual object Insert(TEntity entity)
        {
            if (entity is ISerializableProperties)
            {
                ISerializableProperties properties = entity as ISerializableProperties;
                if (properties != null)
                {
                    properties.Serialize();
                }
            }
            object obj2 = this.CreateDAO().Insert(entity);

            this.OnInserted(entity);
            return(obj2);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 把实体entity添加到数据库
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual object Insert(TEntity entity)
        {
            //处理可序列化属性
            if (entity is ISerializableProperties)
            {
                ISerializableProperties extendedProperties = entity as ISerializableProperties;
                if (extendedProperties != null)
                {
                    extendedProperties.Serialize();
                }
            }

            var id = CreateDAO().Insert(entity);

            OnInserted(entity);

            return(id);
        }
Exemplo n.º 5
0
        public virtual void Update(TEntity entity)
        {
            PetaPocoDatabase petaPocoDatabase = this.CreateDAO();

            if (entity is ISerializableProperties)
            {
                ISerializableProperties serializableProperties = entity as ISerializableProperties;
                if (serializableProperties != null)
                {
                    serializableProperties.Serialize();
                }
            }
            int num;

            if (Repository <TEntity> .RealTimeCacheHelper.PropertyNameOfBody != null && Repository <TEntity> .RealTimeCacheHelper.PropertyNameOfBody.GetValue(entity, null) == null)
            {
                Database.PocoData pocoData = PetaPoco.Database.PocoData.ForType(typeof(TEntity));
                System.Collections.Generic.List <string> list = new System.Collections.Generic.List <string>();
                foreach (System.Collections.Generic.KeyValuePair <string, Database.PocoColumn> current in pocoData.Columns)
                {
                    if (string.Compare(current.Key, pocoData.TableInfo.PrimaryKey, true) != 0 && (SqlBehaviorFlags.Update & current.Value.SqlBehavior) != (SqlBehaviorFlags)0 && string.Compare(current.Key, Repository <TEntity> .RealTimeCacheHelper.PropertyNameOfBody.Name, true) != 0 && !current.Value.ResultColumn)
                    {
                        list.Add(current.Key);
                    }
                }
                num = petaPocoDatabase.Update(entity, list);
            }
            else
            {
                num = petaPocoDatabase.Update(entity);
            }
            if (num > 0)
            {
                this.OnUpdated(entity);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 把实体entiy更新到数据库
        /// </summary>
        /// <param name="entity">实体</param>
        public virtual void Update(TEntity entity)
        {
            Database dao = CreateDAO();

            int affectedRecords;

            //处理可序列化属性
            if (entity is ISerializableProperties)
            {
                ISerializableProperties extendedProperties = entity as ISerializableProperties;
                if (extendedProperties != null)
                {
                    extendedProperties.Serialize();
                }
            }

            //设置实体正文缓存时如果未给实体正文赋值则不更新
            if (RealTimeCacheHelper.PropertyNameOfBody != null && RealTimeCacheHelper.PropertyNameOfBody.GetValue(entity, null) == null)
            {
                var           pd      = PocoData.ForType(typeof(TEntity));
                List <string> columns = new List <string>();
                foreach (var column in pd.Columns)
                {
                    //去掉主键
                    if (string.Compare(column.Key, pd.TableInfo.PrimaryKey, true) == 0)
                    {
                        continue;
                    }

                    //去掉不允许update的属性
                    if ((SqlBehaviorFlags.Update & column.Value.SqlBehavior) == 0)
                    {
                        continue;
                    }

                    //去掉实体正文
                    if (string.Compare(column.Key, RealTimeCacheHelper.PropertyNameOfBody.Name, true) == 0)
                    {
                        continue;
                    }

                    // Dont update result only columns
                    if (column.Value.ResultColumn)
                    {
                        continue;
                    }

                    columns.Add(column.Key);
                }

                affectedRecords = dao.Update(entity, columns);
            }
            else
            {
                affectedRecords = dao.Update(entity);
            }

            if (affectedRecords > 0)
            {
                OnUpdated(entity);
            }
        }