Exemplo n.º 1
0
        private static void InitColumns()
        {
            var columnIndex = 0;
            var columns     = EntityType.GetProperties()
                              .Where(propInfo => propInfo.GetCustomAttribute <NotMappedAttribute>() == null)
                              .ToDictionary((propInfo) => propInfo.Name,
                                            (propInfo) =>
            {
                var column = propInfo.GetCustomAttribute <ColumnAttribute>() ??
                             new ColumnAttribute(propInfo.Name)
                {
                    IsPrimaryKey = String.Equals(propInfo.Name, DEFAULT_ID_NAME,
                                                 StringComparison.CurrentCultureIgnoreCase)
                };
                column.Property = propInfo;
                if (String.IsNullOrEmpty(column.Name))
                {
                    column.Name = propInfo.Name;
                }

                if (column.FieldType == null)
                {
                    column.FieldType = Nullable.GetUnderlyingType(propInfo.PropertyType) ??
                                       propInfo.PropertyType;
                }

                if (propInfo.GetMethod != null && column.GetPropertyValue == null)
                {
                    column.GetPropertyValue = GetAccessorFactory.Create(propInfo);
                }

                if (!column.Ordinal.HasValue)
                {
                    column.Ordinal = columnIndex;
                }

                if (column.IsPrimaryKey)
                {
                    MetaData.PrimaryKey = column;
                }

                columnIndex++;
                return(column);
            });

            MetaData.Columns = new SortedDictionary <string, ColumnAttribute>(columns);
            var indexColMap = MetaData.Columns.ToDictionary((col) =>
            {
                if (!col.Value.Ordinal.HasValue)
                {
                    throw new SmartSqlException(
                        $"Entity:{EntityType.FullName} {col.Value.Name}.Ordinal can not be null.");
                }

                return(col.Value.Ordinal.Value);
            }, col => col.Value);

            IndexColumnMaps = new SortedDictionary <int, ColumnAttribute>(indexColMap);
        }