Пример #1
0
        public TableSchema GetEntitySchema(Type type)
        {
            IEntityProxy proxy       = EntityProxyManager.Instance.GetEntityProxyFromType(type);
            TableSchema  tableSchema = new TableSchema(OrmUtil.GetTableName(type));

            foreach (PropertySchema item in proxy.GetPropertyList())
            {
                ColumnSchema       columnSchema       = new ColumnSchema(item.Name);
                OrmColumnAttribute ormColumnAttribute = item.GetCustomerAttribute <OrmColumnAttribute>();
                if (ormColumnAttribute != null)
                {
                    if (!string.IsNullOrEmpty(ormColumnAttribute.DbColumnName))
                    {
                        columnSchema.ColumnName = ormColumnAttribute.DbColumnName;
                    }

                    columnSchema.IsPrimary  = ormColumnAttribute.PrimaryKeyFlag;
                    columnSchema.IsNullable = ormColumnAttribute.AllowNullFlag;
                }
                columnSchema.Type       = item.PropertyType;
                columnSchema.IsIdentity = string.Compare(columnSchema.ColumnName, "id", true) == 0;
                if (columnSchema.Type == typeof(string))
                {
                    columnSchema.MaxLength = 200;
                }

                columnSchema.IsNullable = true;

                tableSchema.ColumnList.Add(columnSchema);
            }

            return(tableSchema);
        }
Пример #2
0
        /// <summary>
        /// 读取类的标示列
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <returns>主键列名</returns>
        public static string GetIdentityColumnName <T>()
            where T : class
        {
            string result = Constants.DefaultIdentityColumnName;

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

            foreach (PropertySchema schema in entityProxy.GetPropertyList())
            {
                OrmColumnAttribute ormColumnAttribute = schema.GetCustomerAttribute <OrmColumnAttribute>();
                if (ormColumnAttribute != null && ormColumnAttribute.PrimaryKeyFlag)
                {
                    result = schema.Name;
                }
            }

            return(result);
        }