insert into A values (newid(), (select top 1 Id from B where some =
'foo'))
I can execute this sql through the Execute.Sql-method on
FluentMigrator. But I would rather have the best of both worlds and be
able to select Id from B first and then use the FluentMigrator Insert-
API to insert the row in A.
I've read a couple of posts about the Execute.WithConnection-method
which should make this possible. The above example would then be
written like this:
var id = 0;
Execute.WithConnection((c, t) => {
var command = c.CreateCommand();
command.Transaction = t;
command.CommandText = "select top 1 Id from B where some = 'foo'";
using (var reader = command.ExecuteReader()) {
while (reader.Read())
id = int.Parse(reader[0].ToString());
}
});
Insert.IntoTable("A").Row(new {Id = Guid.NewGuid(), BRef = id});
The only problem is, that the above code sample doesn't work. The
migration step added with the WithConnection-method is executed AFTER
the migration step added with the IntoTable-method.
I've also tried adding the Insert.IntoTable(...) call inside the
WithConnection-body. This causes FluentMigrator to throw a
NullReferenceException when calling Insert.IntoTable.
Any ideas how to fix this problem?