This is a migrated thread and some comments may be shown as answers.

WithInfiniteLength producing schema update of length 255 instead of max

1 Answer 34 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Musashi
Top achievements
Rank 1
Musashi asked on 29 Feb 2016, 09:07 PM

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

 

1 Answer, 1 is accepted

Sort by
0
Musashi
Top achievements
Rank 1
answered on 29 Feb 2016, 09:48 PM
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.
Tags
Development (API, general questions)
Asked by
Musashi
Top achievements
Rank 1
Answers by
Musashi
Top achievements
Rank 1
Share this question
or