在传递给Execute.WithConnection的操作内部调用FluentMigrator的构建器方法会导致抛出空引用异常。
我要做的是选择一些数据,这样我就可以在c#中操作它,因为这比在T-SQL中操作它更容易,并使用我的c#操作的结果来更新数据或插入新数据(更具体地说,我需要从存储的url字符串中选择一个查询字符串参数,并将其插入到其他地方)。
我看到的在迁移中选择数据的唯一方法是使用Execute.WithConnection并自己检索数据(FluentMigrator没有提供选择数据的帮助器),但是如果我试图在传递给Execute.WithConnection的操作中使用fluent migrator表达式,就会抛出一个空引用异常。
以下是我的代码的精简版本:
[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"); // this is immediately evaluated, no reader is left open
foreach (var r in records) {
var newValue = Manipulate(r.OriginalValue);
Insert.IntoTable("NewRecords").Row(new { OriginalValueId = r.Id, NewValue = newValue }); // <-- this line causes the exception
}
}
public void Down() {}
}调用Inser.IntoTable的行将导致从FluentMigrator\Builders\Insert\InsertExpressionRoot.cs的第36行抛出一个空异常-此时_context变量似乎可能为空,但我不明白为什么会这样。(当测试Create.Table时,例如,它发生在FluentMigrator\Builders\Create\CreateExpressionRoot.cs的第49行)
任何帮助都将不胜感激。也许在DML是否适合迁移的问题上存在分歧,我对建议持开放态度,但这种情况仅在本周就出现了两次。现在,我只是在操作中使用我的微ORM而不是FluentMigrator来执行插入,这确实有效,但似乎我正在尝试做的事情应该可以工作。
发布于 2013-04-04 05:19:47
使用Execute.WithConnection表达式时,您得到的只是db连接和事务。
使用Execute.WithConnection创建PerformDBOperationExpression表达式。在处理表达式时,处理器调用操作属性(an example in the SqlServerProcessor),并且处理器没有对MigrationContext的引用。但是,即使它能够访问MigrationContext,当FluentMigrator进入处理阶段时,也已经为时已晚。您可能正在尝试处理表达式中的表达式,而目前FluentMigrator还不能处理这种类型的嵌套。
另一种方法是使连接字符串在迁移上下文中可用,请参阅此问题:https://github.com/schambers/fluentmigrator/issues/240
这是一种更好的方法吗?
https://stackoverflow.com/questions/15366162
复制相似问题