首页 > 解决方案 > 使用序列在 Fluent Migrator 中插入值

问题描述

有没有办法Fluent Migrator在数据库中使用我的序列插入一行?

前任。我想执行这个命令:

insert into TEST_TABLE (ID, DUMMY) values (seq_test.nextval, 'TEST') 

我的顺序:

seq_test

我试过的:

Insert.IntoTable("TEST_TABLE").Row(new { /*Sequence Here*/, DUMMY = "Test" });

我该怎么做才能让我的序列下一个 val?

任何帮助,将不胜感激

谢谢!

标签: c#sqlfluent-migrator

解决方案


插入 TEST_TABLE (ID, DUMMY) 值 (seq_test.nextval, 'TEST')

如果这是 oracle 序列

RawSql.Insert("seq_test.nextval from dual"),  should do mix.

Insert.IntoTable("User").Row(new { ID = RawSql.Insert("seq_test.nextval from dual"),DUMMY ='test' });

或者

Insert.IntoTable("User").Row(new { ID = RawSql.Insert("select seq_test.nextval"),DUMMY ='test' });

这将导致将用户名列设置为当前用户名。请注意,通过使用诸如 CURRENT_USER 之类的 sql server 特定函数,此表达式不再可移植,并且无法针对另一个数据库(如 PostgreSQL)工作。

原始 SQL 帮助程序 使用 Insert.IntoTable 表达式或设置默认列值时,所有作为字符串的行数据都会被引用并保存在数据库中。如果您想改用 sql 表达式,那么 RawSql 助手类就是您所需要的。

For example, if I want to use a Microsoft SQL Server function like CURRENT_USER and try to insert like this:

Insert.IntoTable("Users").Row(new { Username = "CURRENT_USER" });
The result will be that the Username column will get the value CURRENT_USER as a string. To execute the function you can use the RawSql.Insert method like this:

Insert.IntoTable("User").Row(new { Username = RawSql.Insert("CURRENT_USER") });
This will result in the Username column being set to the current username. Be aware that by using an sql server specific function like CURRENT_USER that this expression is not portable anymore and will not work against another database (like PostgreSQL).

推荐阅读