I am trying to add columns to a table and then update the current rows inside the same table. I found the Update expression but I can't seem to find how to tell it which column to update. You can see the migration below where I want to update the 'SentInvoice' column of table 'Order'.
public class MigrationAddedStartDateTrainingToOrder : MigrationBase
{
public MigrationAddedStartDateTrainingToOrder(IMigrationContext context)
: base(context)
{
}
public override void Migrate()
{
if (!ColumnExists("Order", "StartDateTraining"))
{
Create.Column("StartDateTraining").OnTable("Order").AsDateTime().Nullable().WithDefaultValue(null).Do();
Create.Column("SentInvoice").OnTable("Order").AsDateTime().Nullable().WithDefaultValue(null).Do();
// Update Order SentInvoice
Update.Table("Order").Set(DateTime.Now).AllRows().Do();
}
}
}
Does anyone know how to declare the column which should be updated?
Migration plan update data in custom table
Hello,
I am trying to add columns to a table and then update the current rows inside the same table. I found the Update expression but I can't seem to find how to tell it which column to update. You can see the migration below where I want to update the 'SentInvoice' column of table 'Order'.
Does anyone know how to declare the column which should be updated?
Hi Joel
It looks like that the 'Set' method on the UpdateDataBuilder takes in the data as an anonymous object
https://github.com/umbraco/Umbraco-CMS/blob/33adbf41fa1f5c5d0759c70a7116114107addf56/src/Umbraco.Infrastructure/Migrations/Expressions/Update/UpdateDataBuilder.cs#L19
And you can see it builds to 'Set Expression' by calling 'getData' method:
https://github.com/umbraco/Umbraco-CMS/blob/33adbf41fa1f5c5d0759c70a7116114107addf56/src/Umbraco.Infrastructure/Migrations/Expressions/Update/UpdateDataBuilder.cs#L39
Which turns the anonymous object into a list of KeyValuePairs of PropertyName and Value and builds up the Set expression...
Sooooooo I think
will do what you need... but if so it's wilfully obscure :-P
If not then looking at the core 'migrations' that Umbraco HQ have created between versions:
https://github.com/umbraco/Umbraco-CMS/tree/33adbf41fa1f5c5d0759c70a7116114107addf56/src/Umbraco.Infrastructure/Migrations/Upgrade/V80_0
Can see they often use Database.Execute to do an update of data, but looking at the code, I think that anonymous object should work.
regards
Marc
Thanks for the leg up, from experimenting I'd have to say that using Database.Execute() is the quickest way to get this working.
is working on a reply...