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

谁都会走 提交于 2019-12-04 14:55:34

问题


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)?


回答1:


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



回答2:


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




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/2496695/can-i-create-a-column-of-nvarcharmax-using-fluentmigrator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!