Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Column Types with configurable varchar lengths for a json migration #1

brightstreetgroup opened this Issue April 16, 2012 · 1 comment

2 participants

BrightStreet Group Rob Conery
BrightStreet Group

if you create a new table and specify "string" as the columnType, it will always default to nvarchar(255). I want to add more control to that, say if you want a 50 character string.

I updated the SetColumnType method and added check so you can pass in "string[50]" for example, and that will generate a nvarchar(50) instead of nvarchar(255).

I'm sure this function can be much simpler with a regex instead.

private string SetColumnType(string colType) {

        // TODO:  I'm sure regex will do a better job at this
        if (colType.IndexOf("[") > 0 && colType.IndexOf("]") > 0)
            // get length value
            int start = colType.IndexOf("[") + 1;
            int end = colType.IndexOf("]");

            string lenVal = colType.Substring(start, end - start);

            return colType = colType.Replace(colType, "nvarchar(" + lenVal + ")");

        return colType.Replace("pk", "int PRIMARY KEY IDENTITY(1,1)")
            .Replace("money", "decimal(8,2)")
            .Replace("date", "datetime")
            .Replace("string", "nvarchar(255)")
            .Replace("boolean", "bit")
            .Replace("text", "nvarchar(MAX)")
            .Replace("guid", "uniqueidentifier");

Here is an example of the json

{name:'SubjectID', type:'pk',null:false},
{name:'SubjectName', type:'string[255]',nullable:false},
{name:'Version', type:'string[50]', nullable:true}

Rob Conery

I believe you can just override by sending in whatever type you like - nvarchar(50) will work just as well as string[50] minus the code changes.

Rob Conery robconery closed this June 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.