IDENTITY_INSERT

853 views
Skip to first unread message

Justin Collum

unread,
Apr 14, 2011, 4:53:21 PM4/14/11
to FluentMigrator Google Group
Is there a way to get IDENTITY_INSERT for a FM migration to SQL
Server? I tried specifying the identity in the Row method call but
that didn't work.

Andrew Busby

unread,
Apr 14, 2011, 5:03:06 PM4/14/11
to fluentmigrato...@googlegroups.com
Not directly.

If you are only going to be executing against Sql Server the you can use the
Execute method to turn on identity insert.

So for example,

Execute.Script("SET IDENTITY_INSERT [dbo].[ Foo] ON");
Insert.IntoTable("Foo") .InSchema("dbo").Row(new { MyId = 100 });
Execute.Script("SET IDENTITY_INSERT [dbo].[ Foo] OFF");

Hope it helps

Andy

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

Justin Collum

unread,
Apr 14, 2011, 5:20:24 PM4/14/11
to FluentMigrator Google Group
Ah, that works well enough. Ideally it'd be part of the interface:
Insert.IntoTable().WithIdentityInsert()... Another thing to add to my
list of things I'd like to add to FM. Now to find the time.

Andrew Benz

unread,
Jun 25, 2012, 12:11:08 PM6/25/12
to fluentmigrato...@googlegroups.com
This functionality is baked in to FluentMigrator as of 1.0.2:

Because this is SQL Server specific, it has been implemented as an extension method in the fluentmigrator.runner assembly.  Here's a SUPER simple example:

using FluentMigrator;
using FluentMigrator.Runner.Extensions;

[Migration(1)]
public class IdentitySample : Migration {
  public override void Up() {
    Insert.IntoTable("Foo")
      .WithIdentityInsert()
      .Row(new { id = 1, name = "Foo 1" });
  }
  public override void Down() {}
Reply all
Reply to author
Forward
0 new messages