1

I'm using a type provider to insert data into a SQL Server database. I want the IDs of the entries I just inserted. In C#, I could access the IDs from "foo" after calling SubmitChanges(). This doesn't happen for me in F#. Does it have something to do with immutability? Is there a way to access these IDs without having to make another call to the DB?

foo
|> dbContext.MyTable.InsertAllOnSubmit
dbContext.DataContext.SubmitChanges()

//data in foo gets added, but Ids are all 0.
//In MyTable, Id is an int identity(1,1) primary key
s952163
  • 6,276
  • 4
  • 23
  • 47

2 Answers2

1

Sorry, I didn't point out that foo was a seq. That appeared to be the issue.

Doing a |> Seq.toList on foo before InsertAllOnSubmit did the trick and I am now able to see IDs.

0

Are you sure about this? I don't know what's in foo but inserting sequences should work. For example:

let dbx = dbSchema.GetDataContext()
let x = seq { 1L..1000L}
let data = seq {for i in  x -> dbSchema.ServiceTypes.Table_1(Number = i)}
dbx.Table_1.InsertAllOnSubmit(data)
dbx.DataContext.SubmitChanges()

Gives me this:

DB Table

Where Number is coming from x, and ID is Primary Key, Identity Column.

EDIT

I see, you want to get the ID from data in this case. Then forcing the enumeration (or keeping the data in an array/list) is the only way I know as well. There might be some options to the DataContext.

let data = [for i in  x -> dbSchema.ServiceTypes.Table_1(Number = i)]
//let data' = data |> Seq.toList
dbx.Table_1.InsertAllOnSubmit(data)
dbx.DataContext.SubmitChanges()

query { for row in data do 
        select row.ID }  

val it : seq = seq [10001L; 10002L; 10003L; 10004L; ...]

s952163
  • 6,276
  • 4
  • 23
  • 47
  • This doesn't answer the question, which asks how to observe the IDs of the newly-inserted records. – ildjarn Jul 23 '16 at 10:29
  • @ildjarn you're right, I misread the Q. OP wants the id from the object not the db. Hmmm.... let me check and delete/update. – s952163 Jul 23 '16 at 12:17