Example #1
0
        /// <summary>
        /// 将实体对象更新到数据库,调用该方法时实体对象必须具备主键属性
        /// </summary>
        /// <param name="object">实体对象</param>
        public virtual int Update(object @object)
        {
            IEntityObject entity = (IEntityObject)@object;

            Type elementType = @object.GetType().BaseType;

            IUpdateable updateable = this.factory.CreateUpdateProvider(this).CreateUpdate(elementType);

            var properties = @object.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            properties.Each(property =>
            {
                if (property.IsPrimaryKey() == false)
                {
                    if (entity.ChangedProperties.Contains(property.Name))
                    {
                        ParameterExpression pe = Expression.Parameter(@object.GetType(), "s");
                        MemberExpression me    = Expression.MakeMemberAccess(pe, property);

                        updateable.Set(me, property.EmitGetValue(@object));
                    }
                }
                else
                {
                    ParameterExpression pe = Expression.Parameter(@object.GetType(), "s");
                    MemberExpression me    = Expression.MakeMemberAccess(pe, property);
                    ConstantExpression ce  = Expression.Constant(property.EmitGetValue(@object), property.PropertyType);
                    BinaryExpression be    = Expression.Equal(me, ce);

                    updateable = updateable.Where(be);
                }
            });

            return(updateable.Execute(elementType));
        }
Example #2
0
        /// <summary>
        /// 设置需更新的实体对象
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="object">需更新的实体对象</param>
        /// <typeparam name="T">元素类型</typeparam>
        public static IUpdateable <T> Set <T>(this IUpdateable <T> source, object @object)
        {
            source.Properties.Clear();

            IEntityObject entity = (IEntityObject)@object;

            Type elementType = @object.GetType().BaseType;

            var properties = @object.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            properties.Where(property => property.IsPrimaryKey() == false).Each(property =>
            {
                if (entity.ChangedProperties.Contains(property.Name))
                {
                    ParameterExpression pe = Expression.Parameter(@object.GetType(), "s");
                    MemberExpression me    = Expression.MakeMemberAccess(pe, property);

                    source.Set(me, property.EmitGetValue(@object));
                }
            });

            return(source);
        }