2

I'm currently using an F# sql type provider to migrate some data between one database to another.

I need to be able to insert identity information (numeric ID column).

The schema for the table has the Id column as being identity.

I've run a script to set identity_insert to ON so that should all be fine.

The issue is that the type-provider-generated insert doesn't actually bother writing the ID value as part of the statement.

Is there a way of forcing it to do so?

1 Answer 1

1

Here's what I did to meet a deadline. It's nowhere near ideal but does insert the record with the 'correct' identity.

mySequence |> Seq.iter (fun x -> 
  db.DataContext.ExecuteCommand(
    "SET IDENTITY_INSERT [dbo].mytable ON 
     insert into mytable (id, name) values({0}, {1}) 
     SET IDENTITY_INSERT [dbo].mytable OFF", x.ID, x.Name) |> ignore)

Edit

Turns out DataContext.ExecuteCommand() will fail if one of the parameters is null because it can't infer the type and assumes Object

I ended up switching to Seekwell (nuget package I wrote) which actually looks a bit cleaner too, given you don't have to deal with parameter ordering.

let command = new Seekwell.Command(destination)
mySequence |> Seq.iter (fun x -> 
  command.ExecuteScalar(
    "SET IDENTITY_INSERT mydb.dbo.mytable ON 
     insert into mydb.dbo.mytable (id, name) values(@ID, @Name) 
     SET IDENTITY_INSERT mydb.dbo.mytable OFF", x) |> ignore)
3
  • Haha yeah, that's pretty much identical to what I ended up doing, except I ended up building the string up by using quotations to declare which properties & columns I wanted to make it in as part of the insert.
    – Clint
    Mar 27, 2015 at 13:55
  • 1
    yep, turns out there's an issue if a property is null. I've switched to using Seekwell. Updating with an edit so that hopefully someone else doesn't run into the same thing.
    – Reid Evans
    Mar 27, 2015 at 17:05
  • I had something, that took the identity insert block, and folded a collection of PropertyInfo quotations into the "names" and "values" sections and dealt with things like dropping null value properties etc. Was a major trial and error job haha. Basically: gist.github.com/ClintPearson-KPMG/f245277d0f31d5b4c58c Part of a larger "generic-ish" app that does migration, and allowed skipping based on other items that have been skipped etc.
    – Clint
    Mar 27, 2015 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.