开发者

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

开发者 https://www.devze.com 2022-12-23 15:42 出处:网络
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 开发者_如何学Pythoncolu

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 开发者_如何学Pythoncolumn of type nvarchar(MAX)?


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);
    }
}


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...


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


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


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)");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号