Re: Using fluent expressions inside Execute.WithConnection action

1,489 views
Skip to first unread message

Quentin Starin

unread,
Mar 20, 2013, 5:36:04 PM3/20/13
to fluentmigrato...@googlegroups.com
Nobody has any clue what I'm taking about, or what?


On Friday, March 8, 2013 4:31:20 PM UTC-6, Quentin Starin wrote:
Is it possible to call the fluent expression methods while inside the action that I pass to Execute.WithConnection? I am trying to use Insert.IntoTable and get a null reference exception thrown from line 36 of FluentMigrator\Builders\Insert\InsertExpressionRoot.cs - it appears that the _context variable may be null at this point I am having a difficult time understanding why this is.

What I am trying to do is select some data so that I may manipulate it in c#, as that is easier than manipulating it in T-SQL, and use the result of my c# operations to update the data or insert new data (to be more specific, I need to pick one query string parameter out of a stored url string and insert it somewhere else). 

The only way I see to select data within a migration is to use Execute.WithConnection, but if I try to use any fluent migrator expression I get this null reference exception (when testing Create.Table, e.g., it occurs on line 49 of FluentMigrator\Builders\Create\CreateExpressionRoot.cs).

Here's a simple code example of what I'm trying to do, the null reference exception is thrown from the line in CustomDml that calls Insert.IntoTable:

[Migration(1)]
public class MyMigration : Migration 
{
  public void Up() 
  {
    Execute.WithConnection(CustomDml);
  }

  public void CustomDml(IDbConnection conn, IDbTransaction tran)
  {
    var db = new NPoco.Database(conn).SetTransaction(tran); // NPoco is a micro-ORM, a fork of PetaPoco
    var records = db.Fetch<Record>("-- some sql");
    foreach (var r in records) {
      var newValue = Manipulate(r.OriginalValue);
      Insert.IntoTable("NewRecords").Row(new { OriginalValueId = r.Id, NewValue = newValue });
    }
  }

  public void Down() {}
}

Any help would be appreciated. Perhaps there is disagreement on whether DML is appropriate in a migration, and I am open to suggestions, but this scenario has come up twice this week alone. For now I am simply performing the insert using my micro-ORM within the action rather than FluentMigrator and that does work. Thanks.

Jeff Treuting

unread,
Mar 20, 2013, 6:42:35 PM3/20/13
to fluentmigrato...@googlegroups.com
I don't think that is currently possible.  Whenever I've used Execute.WithConnection, I've just committed to using SQL throughout it.

I would say that using your micro-ORM for the insert seems like a good way to go.  As I see it, the Insert.IntoTable helps you get away from writing raw SQL, but once you use the Execute.WithConnection and start writing raw SQL for the fetch then you might as well stay with it for that migration.

If using NPoco for the fetch still allows you to switch between database backends easily then using SQL for the insert won't stop that from happening either.


--
You received this message because you are subscribed to the Google Groups "FluentMigrator Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fluentmigrator-goog...@googlegroups.com.
To post to this group, send email to fluentmigrato...@googlegroups.com.
Visit this group at http://groups.google.com/group/fluentmigrator-google-group?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Jeff Treuting
Truburn Solutions, LLC
(310) 295-4683 
www.truburn.net
je...@truburn.net


Daniel Lee

unread,
Apr 3, 2013, 5:28:23 PM4/3/13
to fluentmigrato...@googlegroups.com
Like Jeff says you're better off using PetaPoco here. FluentMigrator expressions would not be processed from within the action in the Execute.WithConnection expression even if they had access to the MigrationContext. The MigrationRunner has no way of accessing nested expressions (expressions that are inside the PerformDBOperationExpression) and therefore cannot execute them.

Reply all
Reply to author
Forward
0 new messages