Is it possible to create an nvarchar column (MAX) using FluentMigrator?

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

+41
fluent-migrator
Mar 23 '10 at 0:20
source share
4 answers

You can create an extension method for wrap.AsString (Int32.MaxValue) inside .AsMaxString ()

eg.

 internal static class MigratorExtensions { public static ICreateTableColumnOptionOrWithColumnSyntax AsMaxString(this ICreateTableColumnAsTypeSyntax createTableColumnAsTypeSyntax) { return createTableColumnAsTypeSyntax.AsString(int.MaxValue); } } 
+54
Jul 04 '10 at 15:30
source share

OK, I found it. Basically, use .AsString (Int32.MaxValue). It is a pity that there is no .AsMaxString () method, but I think it is easy enough to insert ...

+20
Mar 23
source share

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

+13
Mar 20 2018-12-12T00:
source share

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

For example, almost every of my tables has CreateAt and UpdateAt DateTime columns, so I hacked into a small extension method, so I can say:

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

I think I created the extension method correctly ... I know this works, but FluentMigrator has many 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, almost 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 this Id for me. Not sure ... I actually started using FluentMigrator today, but you should always be refactoring whenever possible!

NOTE. . If you are doing helper / extension methods for your migrations, you should never ever change what those methods do. If you do this, someone might try to start your migrations, and everything could explode, because the helper methods that you used to create migration # 1 work differently than before.

Here is the code to create the columns to help you create helper methods: https://github.com/schambers/fluentmigrator/blob/master/src/FluentMigrator/Builders/Create/Column/CreateColumnExpressionBuilder.cs

+4
Apr 04 2018-11-11T00:
source share



All Articles