Beispiel #1
0
        /// <summary>
        /// Insert An Item to DB
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public Boolean InsertItem(ItemDetail o)
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                try
                {
                    o.ID     = Guid.NewGuid().ToString();
                    o.Status = 1; // Default is Active
                    if (o.Temp1 == null)
                    {
                        o.Temp1 = "";
                    }
                    if (o.Temp2 == null)
                    {
                        o.Temp2 = "";
                    }

                    db.ItemDetails.Add(o);
                    db.SaveChanges();
                    transaction.Complete();
                    return(true);
                }
                catch (Exception ex)
                {
                    transaction.Dispose();
                    return(false);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Update Item Detail's Infomation
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public bool UpdateItem(ItemDetail o, params System.Linq.Expressions.Expression <Func <ItemDetail, object> >[] properties)
        {
            var item = db.ItemDetails.Where(i => i.ID == o.ID).SingleOrDefault();

            if (item != null)
            {
                foreach (var propertie in properties)
                {
                    var lambda = (LambdaExpression)propertie;
                    MemberExpression memberExpression;
                    if (lambda.Body is UnaryExpression)
                    {
                        memberExpression = (MemberExpression)((UnaryExpression)lambda.Body).Operand;
                    }
                    else
                    {
                        memberExpression = (MemberExpression)lambda.Body;
                    }

                    string propertyName = memberExpression.Member.Name;
                    item.GetType().GetProperty(propertyName).SetValue(item, o.GetType().GetProperty(propertyName).GetValue(o));
                }
                db.SaveChanges();
            }
            else
            {
                return(false);
            }
            return(true);
        }