AutoPocoIO / AutoPocoIO

Auto generate POCO classes at runtime for use in .NET Framework & .NET Core applications. No DbContext required; classes are created from database schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Column attribute can have invalid datatype: "varchar(-1)" instead of "varchar(max)"

TamerRifaiIntellectix opened this issue · comments

commented

I branched off of dev and committed locally a fix for this as well as improvements to make the column attribute more detailed and include other datatypes and order. I don't have permission to push. I was going to just push and do a pull request and not check anything directly on dev or main branch. Just wanted to throw it out there. In any case, here is a summary of what I did:

Replaced lines around 264 of DynamicClassBuilder.cs where column attribute was created with:

            attribute = GetColumnAttribute(column);

Then created method.

    private static CustomAttributeBuilder GetColumnAttribute(Column column)
    {
        string dataType;

        switch (column.ColumnType.ToUpperInvariant())
        {
            case "NVARCHAR":
            case "CHAR":
            case "NCHAR":
            case "VARCHAR":
            case "VARCHAR2":
            case "VARBINARY":
            case "BINARY":
                dataType = $"{column.ColumnType}({(column.ColumnLength == -1 ? "max" : column.ColumnLength.ToString(CultureInfo.InvariantCulture))})";
                break;
            case "FLOAT":
                dataType = $"{column.ColumnType}({column.ColumnPercision})";
                break;
            case "NUMERIC":
            case "DECIMAL":
                dataType = $"{column.ColumnType}({column.ColumnPercision},{column.ColumnScale})";
                break;
            case "DATETIME2":
            case "DATETIMEOFFSET":
            case "TIME":
                dataType = $"{column.ColumnType}({column.ColumnScale})";
                break;
            default:
                dataType = column.ColumnType;
                break;
        }

        ConstructorInfo ColumnAttributeBuilder = typeof(ColumnAttribute).GetConstructor(new Type[] { typeof(string) });
        var typePropertyInfo = typeof(ColumnAttribute).GetProperties().FirstOrDefault(o => o.Name == nameof(ColumnAttribute.TypeName));
        var orderPropertyInfo = typeof(ColumnAttribute).GetProperties().FirstOrDefault(o => o.Name == nameof(ColumnAttribute.Order));

        return new CustomAttributeBuilder(ColumnAttributeBuilder, new object[] { column.ColumnName }, new PropertyInfo[] { typePropertyInfo, orderPropertyInfo }, new object[] { dataType, column.ColumnOrder});
    }

Added 3 new properties to Column model: ColumnScale, ColumnPercision, and ColumnOrder.

Updates query in MsSqlSchmeaQueries.cs to include the new properties.

Updates DbSchemaBuilderBase.cs to include the properties.

Updated tests including adding a new test for decimal types that confirm percision and scale.

Attached updated files in zip.

ColumnAttributeFix.zip