Updating row data with a new value for each row using fluentmigrator

I am using fluentmigrator to add a new column to a table. Then I want to update each row in the table with a unique value for this column.

Currently, when I use:

Update.Table("Foo").InSchema("dbo").Set(new { Bar = Bar.Generate() }).AllRows(); 

It gives the same value for all rows.

How to ensure that it calls this method for each row?

+5
c # fluent-migrator
Sep 04
source share
1 answer

I'm not sure what Bar.Generate does, but I assume that it creates a GUID or a unique identifier.

If so, you can use:

 Execute.Sql("update dbo.Foo set Bar = NEWID()"); 

Or, if you need sequential loops, you can use NEWSEQUENTIALID () .

If you add a new column for this unique identifier, all you have to do is specify a new column .AsGuid ()

EDIT: FluentMigrator is a small free dsl and is not intended to cover a complex case like this. There is no way (as far as I know) to do this with a single sql UPDATE, and therefore there is no easy way to do this with FluentMigrator. You will need to count the number of rows for the table using ADO.NET or ORM (Dapper / NHibernate), and then skip each row and update the Bar column with a unique unique identifier. Therefore, if you have a million rows, you will need to do a million sql updates. If you can rewrite your Bar.Generate () method as an Sql function that is based on a NEWID () function such as this or this , then you can do it as a single UPDATE statement and call it using the FluentMigrator Execute.Sql method.

You did not indicate which database you are working with. But some like Postgres have non-standard features that may help you.

+7
Sep 04 '12 at 15:39
source share



All Articles