WithInfiniteLength producing schema update of length 255 instead of max

2 posts, 0 answers
  1. Musashi
    Musashi avatar
    20 posts
    Member since:
    Jan 2014

    Posted 29 Feb Link to this post

    I have an existing table with a column of type "nvarchar(max) NULL". I'm trying to make sure that my code first metadata doesn't alter anything starting up. But for the life of me I can't figure out why the update code that gets generated makes "nvarchar(255) null".

    Here's the method in question.

    public static void PrepareStringProperty<T>(this MappingConfiguration<T> configuration, Expression<Func<T, string>> propExp, int maxLength, bool isRequired, bool isUnicode)
    {
        string propName = ObjectExtensions.GetPropertyName(propExp);
        string columnType = isUnicode ? Provider.GetTypeUnicodeString() : Provider.GetTypeString();
        var propConfig = configuration.HasProperty(propExp).HasFieldName(FindFieldName<T>(propName)).WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn(propName).HasColumnType(columnType);
        if (isRequired)
            propConfig.IsNotNullable();
        else
            propConfig.IsNullable();
     
        if (isUnicode)
            propConfig.IsUnicode();
        else
            propConfig.IsNotUnicode();
     
        if (maxLength > 0)
            propConfig.WithVariableLength(maxLength);
        else
            propConfig.WithInfiniteLength();
    }

    The supplied parameters are maxLength = 0, isRequired = false, isUnicode = true.

    The end result should be a call such as the following, which is verified during debug:
    configuration.HasProperty(propExp).HasFieldName(FindFieldName<T>(propName)).WithDataAccessKind(DataAccessKind.ReadWrite).ToColumn(propName).HasColumnType(columnType).IsNullable().IsUnicode().WithInfiniteLength();

    Nevertheless, the update script that is generated looks like this:

    -- Column was read from database as: [CMSVALUE] nvarchar(max) null
    -- modify column for field _cMSVALUE
    ALTER TABLE [CMSTRANS] ALTER COLUMN [CMSVALUE] nvarchar(255) NULL

     

  2. Musashi
    Musashi avatar
    20 posts
    Member since:
    Jan 2014

    Posted 29 Feb in reply to Musashi Link to this post

    Ok, I found my problem. Specifying my own column type, even if it's correct, overrides the infinite length bit. Reason for specifying my own column type is to handle multiple databases. But if Data Access can fill in the blanks automatically, then I guess I don't need to specify my own column type, at least for strings.
  3. DevCraft banner
Back to Top