How do I add new column with default value in Migrator.Framework in C#?

1.6k views Asked by At

I am using code similar to the following:

 Database.AddColumn(
             "TableName",
             new Column(
                        "ColumnName",
                        DbType.String,
                        ColumnProperty.NotNull,
                        "TypeName"));

But I am getting error as "TypeName" is not allowed in this context, allowed context are constants, constants expression but no column name.

2

There are 2 answers

0
Lance Fisher On

For strings, you have to put single quotes around the default value as the ALTER statement is built with it directly. Try this:

Database.AddColumn(
         "TableName",
         new Column(
                    "ColumnName",
                    DbType.String,
                    ColumnProperty.NotNull,
                    "'TypeName'"));
0
danyolgiax On

Have you tried this way?

Database.AddColumn("table", new Column("colName", DbType.String, "defaultValue"));