每使用行fluentmigrator一个新的值更新行数据 [英] Update row data with a new value per row using fluentmigrator

查看:210
本文介绍了每使用行fluentmigrator一个新的值更新行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用fluentmigrator一个新列添加到表中。然后我想更新与该列的唯一值的表的每一行

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

目前,当我使用:

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



它给了所有行相同的值。

It gives the same value for all the rows.

我如何确保它调用的每一行的方法?

How do I ensure it calls that method for each row?

推荐答案

我不知道什么Bar.Generate 。的确,但我猜测它会创建一个GUID或唯一的id

I'm not sure what Bar.Generate does but I am guessing it creates a GUID or unique id.

如果这样,那么你可以使用:

If so then you could use:

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



或者,如果你想连续的GUID,那么你可以使用的 NEWSEQUENTIALID()

如果要添加新列这个独特的identier,那么所有你需要做的就是指定新列.AsGuid()

If you are adding a new column for this unique identier, then all you would need to do is specify the new column .AsGuid()

编辑:FluentMigrator是一个小流利的DSL,并不意味着覆盖一个复杂的情况是这样的。有没有办法(据我所知)一个SQL UPDATE,因此没有简单的方法与FluentMigrator做到这一点做到这一点。你必须得到使用ADO.NET或ORM(小巧玲珑/ NHibernate的),然后遍历每行表中的行数和更新与自定义唯一标识符栏列。所以,如果你有一百万行,那么你将不得不作出百万SQL更新。如果你可以重写你的Bar.Generate()方法,因为这是基于NEWID()之类的这个或的这个那么你可以做它为一个UPDATE语句和FluentMigrator的Execute.Sql方法调用它。

FluentMigrator is a small fluent dsl and is not meant to cover a complicated case like this. There is no way (as far as I know) to do this with one sql UPDATE and therefore no easy way to do it with FluentMigrator. You'll have to get the row count for the table with ADO.NET or an ORM (Dapper/NHibernate) and then loop through each row and update the Bar column with the custom unique identifier. So if you have one million rows then you will have to make one million sql updates. If you can rewrite your Bar.Generate() method as an Sql function that is based on the NEWID() function like this or this then you could do it as one UPDATE statement and call it with FluentMigrator's Execute.Sql method.

您还没有提到你正在使用哪个数据库。但是一些喜欢的Postgres有非标准功能,可以帮助你。

You haven't mentioned which database you are working with. But some like Postgres have non-standard features that could help you.

这篇关于每使用行fluentmigrator一个新的值更新行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆