.net - SQL: "System.Data.Linq.DuplicateKeyException" -


i new programming. familiarize myself f# , sql server, decided try implementing code found here. have pasted below convenience of not want click on link:

let newrecord = new dbschema.servicetypes.table1(id = 100,                                                  testdata1 = 35,                                                   testdata2 = 2.0,                                                  name = "testing123") let newvalues =     [ in [1 .. 10] ->           new dbschema.servicetypes.table3(id = 700 + i,                                            name = "testing" + i.tostring(),                                            data = i) ] // insert new data database. db.table1.insertonsubmit(newrecord) db.table3.insertallonsubmit(newvalues) try     db.datacontext.submitchanges()     printfn "successfully inserted new rows."    | exn -> printfn "exception:\n%s" exn.message 

i created 2 tables, named table1 , table 3, in sql server. initially, set primary key in each table id. running code above returned output:

exception: cannot add entity key in use.

i started randomly trying various ways of resolving issue (bad practice, know!), based on information found googling exception. 1 approach took change primary key in each table "name" instead. time, no exception thrown.

my problem solved, not understand how or why. questions are:

  1. why exception thrown initially? did have fact set "id" primary key?
  2. did fact change primary key "name" eradicate error? if so, how , why did make difference?

thanks in advance help. please let me know if question insufficiently detailed.

this looks should work - id should unique.

here debug pointers:

  1. get raw interface sql can type queries yourself.

  2. make sure table empty before run code.

  3. test system typing in raw sql in sql client see if works.

good luck.


Comments