BinaryCat
BinaryCat

Reputation: 1300

OrchardCMS - Adding a column to table using Migrations.cs

I'm still fairly new to OrchardCMS development and I was able to create a table and a few columns using the Migrations.cs file. Shortly after the new table and columns were created, it dawned on me that I actually needed a couple more columns added to the same table.

For my initial Migrations.cs, I have something similar to below (irrelevant parts of code not shown for brevity):

// some code above
SchemaBuilder.CreateTable("ProductPartRecord", table => table
    .ContentPartRecord()
    .Column<decimal>("UnitPrice")
    .Column<string>("Sku", column => column.WithLength(50))
    );
return 1;
// some code below

To add additional column(s), I would need to create a class as follows based on my limited knowledge:

public int UpdateFrom1()
{
    SchemaBuilder.AlterTable("ProductPartRecord", table => table
        .AddColumn<string>("UnitColor", c => c.Nullable())
    );
    return 2;
}

But, here comes the question: Why couldn't I simply modify the initial CreateTable method by inserting something like: .Column<string>("UnitColor") instead of creating the UpdateFrom1() class?

Furthermore, let's say the software requirement changes later and now I need to add yet another column to the same table. Would I then create yet another class? (e.g., UpdateFrom2() and so on...) Seems rather odd and I'd like to think there's a better/clever way but maybe it's just a Orchard thing.

Thanks in advance for any tips/advice!

Upvotes: 0

Views: 1083

Answers (2)

Amin Golmahalleh
Amin Golmahalleh

Reputation: 4206

You should add one unit to UpdateFrom and one unit to return for each change.

 //Migrations.cs              

public int UpdateFrom2()
{
    SchemaBuilder.AlterTable("ProductPartRecord", table => table
        .AddColumn<string>("UnitColor", c => c.Nullable())
    );
    return 3;
}

Upvotes: 1

krbnr
krbnr

Reputation: 160

Yes, that's the way that Orchard works as far as I know, the migrations are stored as versions so you'll need to add UpdateFrom2().. for the next migration and so on.

Regards.

Upvotes: 2

Related Questions