示例#1
0
        /// <summary>
        /// 保存数据。
        /// 默认的表名就是类名, 若T类型上有指定的表名,则以此为准
        /// 默认的自增列为Id,若T类型上有指定的自增列, 则以此为准。
        /// 若value中的标识列大于0, 则修改。 若小于等于0, 则新增。
        /// 注:主键为int
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="value">类型值</param>
        /// <returns>新增则返回自增列, 修改则返回本身标示列值。</returns>
        public int Save <T>(T value) where T : class
        {
            ThrowExceptionUtil.ArgumentNotNull(value, "value");

            string identityColumnName = OrmUtil.GetIdentityColumnName <T>();

            string tableName = OrmUtil.GetTableName <T>();

            EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>();

            object identity = entityProxy.GetPropertyValue(value, identityColumnName);

            ThrowExceptionUtil.ArgumentConditionTrue(identity != null, string.Empty, "未指定主键列");

            if ((int)identity <= 0)
            {
                return(Insert <T>(value));
            }
            else
            {
                BeeDataAdapter dataAdapter = BeeDataAdapter.From <T>(value);
                dataAdapter.RemoveKey(identityColumnName);

                Update(tableName, dataAdapter, SqlCriteria.New.Equal(identityColumnName, identity));

                return((int)identity);
            }
        }
示例#2
0
        /// <summary>
        /// Get the property value of the instance.
        /// </summary>
        /// <typeparam name="T">the type of instance.</typeparam>
        /// <param name="src">the instance.</param>
        /// <param name="propertyName">the property name. ignore the case sensitive.</param>
        public static object GetPropertyValue <T>(T src, string propertyName)
            where T : class
        {
            EntityProxy <T> proxy = EntityProxyManager.Instance.GetEntityProxy <T>();

            return(proxy.GetPropertyValue(src, propertyName));
        }
示例#3
0
        private void CreateIndexValue(List <T> list)
        {
            IList indexList = GetIndexList();

            if (indexList == null)
            {
                return;
            }
            TypeCode typeCode = Type.GetTypeCode(indexMeta.PropertyType);

            foreach (T item in list)
            {
                switch (typeCode)
                {
                case TypeCode.Int32:
                    indexList.Add(new IntIndexValue <T>()
                    {
                        IndexValue = (int)(proxy.GetPropertyValue(item, indexMeta.PropertyName))
                        , Value    = item
                    });
                    break;

                case TypeCode.Int64:
                    indexList.Add(new LongIndexValue <T>()
                    {
                        IndexValue = (long)(proxy.GetPropertyValue(item, indexMeta.PropertyName)),
                        Value      = item
                    });
                    break;

                case TypeCode.DateTime:
                    indexList.Add(new LongIndexValue <T>()
                    {
                        IndexValue = ((DateTime)(proxy.GetPropertyValue(item, indexMeta.PropertyName))).Ticks,
                        Value      = item
                    });
                    break;

                case TypeCode.String:
                    indexList.Add(new StringIndexValue <T>()
                    {
                        IndexValue = (string)(proxy.GetPropertyValue(item, indexMeta.PropertyName)),
                        Value      = item
                    });
                    break;
                }
            }

            switch (typeCode)
            {
            case TypeCode.Int32:
                List <IntIndexValue <T> > intIndexList = indexList as List <IntIndexValue <T> >;
                intIndexList.Sort(IntIndexValue <T> .Comparison);
                break;

            case TypeCode.Int64:
            case TypeCode.DateTime:
                List <LongIndexValue <T> > longIndexList = indexList as List <LongIndexValue <T> >;
                longIndexList.Sort(LongIndexValue <T> .Comparison);
                break;

            case TypeCode.String:
                List <StringIndexValue <T> > stringIndexList = indexList as List <StringIndexValue <T> >;
                stringIndexList.Sort(StringIndexValue <T> .Comparison);
                break;
            }
        }