Can I create a column of nvarchar(MAX) using FluentMigrator?
Asked Answered
D

5

62

Using FluentMigrator, the default creation of a Column using .AsString() results in an nvarchar(255). Is there a simple way (before I modify the FluentMigrator code) to create a column of type nvarchar(MAX)?

Daina answered 23/3, 2010 at 0:20 Comment(0)
H
82

You could create an extension method to wrap .AsString(Int32.MaxValue) within .AsMaxString()

e.g.

internal static class MigratorExtensions
{
    public static ICreateTableColumnOptionOrWithColumnSyntax AsMaxString(this ICreateTableColumnAsTypeSyntax createTableColumnAsTypeSyntax)
    {
        return createTableColumnAsTypeSyntax.AsString(int.MaxValue);
    }
}
Harijan answered 4/7, 2010 at 15:30 Comment(0)
D
35

OK, I found it. Basically, use .AsString(Int32.MaxValue). Pity there's not a .AsMaxString() method, but I guess it's easy enough to put in...

Daina answered 23/3, 2010 at 0:47 Comment(1)
that's a brilliant idea. As the current maintainer of FluentMigrator, even I forget how to do this. github.com/fluentmigrator/fluentmigrator/issues/1310Batts
V
28

You can use AsCustom("nvarchar(max)") and pack it to extension

Votive answered 20/3, 2012 at 4:52 Comment(1)
I prefer this option as it is clearly being defined as a sql nvarchar(max), rather than passing an Int32.MaxValue.Sollars
M
8

If you often create columns/tables with the same settings or groups of columns, you should be creating extension methods for your migrations!

For example, nearly every one of my tables has CreatedAt and UpdatedAt DateTime columns, so I whipped up a little extension method so I can say:

Create.Table("Foos").
    WithColumn("a").
    WithTimestamps();

I think I created the Extension method properly ... I know it works, but FluentMigrator has a LOT of interfaces ... here it is:

public static class MigrationExtensions {
    public static ICreateTableWithColumnSyntax WithTimestamps(this ICreateTableWithColumnSyntax root) {
        return root.
            WithColumn("CreatedAt").AsDateTime().NotNullable().
            WithColumn("UpdatedAt").AsDateTime().NotNullable();
    }
}

Similarly, nearly every one of my tables has an int primary key called 'Id', so I think I'm going to add Table.CreateWithId("Foos") to always add that Id for me. Not sure ... I actually just started using FluentMigrator today, but you should always be refactoring when possible!

NOTE: If you do make helper/extension methods for your migrations, you should never ever ever change what those methods do. If you do, someone could try running your migrations and things could explode because the helper methods you used to create Migration #1 works differently now than they did earlier.

Here is the code for creating columns incase it helps you create helper methods: https://github.com/schambers/fluentmigrator/blob/master/src/FluentMigrator/Builders/Create/Column/CreateColumnExpressionBuilder.cs

Medrano answered 4/4, 2011 at 23:38 Comment(1)
i know its an old answer but i must of gone through several of fluent migrators interfaces except the one in your answers, thanks!!Magnetostriction
S
1

How about extending like this:

public static class StringMaxMigratorExtensions
{
    public static ICreateTableColumnOptionOrWithColumnSyntax AsStringMax(this ICreateTableColumnAsTypeSyntax createTableColumnAsTypeSyntax)
    {
        return createTableColumnAsTypeSyntax.AsCustom("nvarchar(max)");
    }

    public static IAlterColumnOptionSyntax AsStringMax(this IAlterColumnAsTypeSyntax alterColumnAsTypeSyntax)
    {
        return alterColumnAsTypeSyntax.AsCustom("nvarchar(max)");
    }
}
Sollars answered 15/1, 2021 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.