예제 #1
0
        private string GetFullyQualifiedOrderByColumnName()
        {
            if (ColumnInfos.Any())
            {
                var columnInfo       = GetDataBoundColumnInfosNotIgnored()[_orderByColumnIndex];
                var linkedTableInfos = GetLinkedTableInfos();
                var parentType       = typeof(T);
                var columnName       = columnInfo.Data;
                var linkedTable      = linkedTableInfos.FirstOrDefault(x => x.ColumnName == columnName);

                if (linkedTable != null)
                {
                    return($"[{linkedTable.LinkedTableAlias}].[{linkedTable.LinkedTableColumnName}]");
                }

                return($"[{parentType.Name}].[{columnName}]");
            }

            return(string.Empty);
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="tablenameMapper"></param>
        public TableInfo(Type type)
        {
            ClassType = type;

            //NOTE: This as dynamic trick should be able to handle both our own Table-attribute as well as the one in EntityFramework
            var tableAttr = type
                            .GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic;

            if (tableAttr != null)
            {
                TableName = tableAttr.Name;
                if ((!(tableAttr is TableAttribute)) && tableAttr.Schema != null)
                {
                    SchemaName = tableAttr.Schema;
                }
            }
            else
            {
                TableName = type.Name;
                if (type.IsInterface() && TableName.StartsWith("I"))
                {
                    TableName = TableName.Substring(1);
                }
            }


            ColumnInfos = type.GetProperties()
                          .Where(t => t.GetCustomAttributes(typeof(IgnoreAttribute), false).Count() == 0)
                          .Select(t =>
            {
                var columnAtt = t.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "ColumnAttribute") as dynamic;

                var ci = new ColumnInfo
                {
                    Property     = t,
                    ColumnName   = columnAtt?.Name ?? t.Name,
                    PropertyName = t.Name,
                    IsKey        = t.GetCustomAttributes(true).Any(a => a is KeyAttribute),
                    IsIdentity   = t.GetCustomAttributes(true).Any(a => a is DatabaseGeneratedAttribute &&
                                                                   (a as DatabaseGeneratedAttribute).DatabaseGeneratedOption == DatabaseGeneratedOption.Identity),
                    IsGenerated = t.GetCustomAttributes(true).Any(a => a is DatabaseGeneratedAttribute &&
                                                                  (a as DatabaseGeneratedAttribute).DatabaseGeneratedOption != DatabaseGeneratedOption.None),
                    ExcludeOnSelect = t.GetCustomAttributes(true).Any(a => a is IgnoreSelectAttribute)
                };

                ci.ExcludeOnInsert = ci.IsGenerated ||
                                     t.GetCustomAttributes(true).Any(a => a is IgnoreInsertAttribute) ||
                                     t.GetCustomAttributes(true).Any(a => a is ReadOnlyAttribute);

                ci.ExcludeOnUpdate = ci.IsGenerated ||
                                     t.GetCustomAttributes(true).Any(a => a is IgnoreUpdateAttribute) ||
                                     t.GetCustomAttributes(true).Any(a => a is ReadOnlyAttribute);

                return(ci);
            })
                          .ToArray();

            if (!ColumnInfos.Any(k => k.IsKey))
            {
                var idProp = ColumnInfos.FirstOrDefault(p => string.Equals(p.PropertyName, "id", StringComparison.CurrentCultureIgnoreCase));

                if (idProp != null)
                {
                    idProp.IsKey = idProp.IsGenerated = idProp.IsIdentity = idProp.ExcludeOnInsert = idProp.ExcludeOnUpdate = true;
                }
            }
        }