按顺序在Fluent Migrator中插入值

时间:2019-06-14 17:28:59

标签: c# sql 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" });

我该怎么做才能让我的下一个序列值生效?

任何帮助将不胜感激

谢谢!

2 个答案:

答案 0 :(得分:1)

插入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服务器特定功能,该表达式不再具有可移植性,并且不能与另一个数据库(如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).

答案 1 :(得分:0)

Insert.IntoTable("TEST_TABLE ").Row(new { SEQ_NO = RawSql.Insert(0), DUMMY = "TEST"});