Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Problem with Unique Identifier column in SQl Server compact edition

Problem with Unique Identifier column in SQl Server compact edition

Scheduled Pinned Locked Moved LINQ
csharpdatabasesql-serverlinqsysadmin
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 3055467
    wrote on last edited by
    #1

    Hi, I have a column of type unique Identifier. I want to insert data into that column. it is working fine for the first time when there are no records, but giving problem 'Cannot add an entity with a key that is already in use' from next time onwards. I am using LINQ in .NET 3.5 and the dbase is SQL Server CE 3.5. Thanks in advance

    H 1 Reply Last reply
    0
    • U User 3055467

      Hi, I have a column of type unique Identifier. I want to insert data into that column. it is working fine for the first time when there are no records, but giving problem 'Cannot add an entity with a key that is already in use' from next time onwards. I am using LINQ in .NET 3.5 and the dbase is SQL Server CE 3.5. Thanks in advance

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      question 1: are you setting the GUID from code, or is it set in SQL server as a default? question 2: if set from code, are you definitely generating a new GUID for each one or re-using the old one? question 3: if set in SQL, be aware that LINQ to SQL ignores SQL default values, and if your column permits null will insert a new record as null

      'Howard

      U 1 Reply Last reply
      0
      • H Howard Richards

        question 1: are you setting the GUID from code, or is it set in SQL server as a default? question 2: if set from code, are you definitely generating a new GUID for each one or re-using the old one? question 3: if set in SQL, be aware that LINQ to SQL ignores SQL default values, and if your column permits null will insert a new record as null

        'Howard

        U Offline
        U Offline
        User 3055467
        wrote on last edited by
        #3

        Hi, Thanks for your reply 1. The GUID is set in sql while designing the table. 2. The column is a having a PK. It doesnt allow nulls. 3. My requirement is the value in the column should be generated automatically like an identity column when I insert a new record. I have tried using NEWID() for default value. It's working for the first time when there are no records. From next time onwards it's giving error. I think its generating same value(000000.......) every time.

        H 1 Reply Last reply
        0
        • U User 3055467

          Hi, Thanks for your reply 1. The GUID is set in sql while designing the table. 2. The column is a having a PK. It doesnt allow nulls. 3. My requirement is the value in the column should be generated automatically like an identity column when I insert a new record. I have tried using NEWID() for default value. It's working for the first time when there are no records. From next time onwards it's giving error. I think its generating same value(000000.......) every time.

          H Offline
          H Offline
          Howard Richards
          wrote on last edited by
          #4

          If the GUID is set using a SQL DEFAULT it won't work with LINQ to SQL unless you flag the primary key as IsDbGenerated = true in the LINQ to SQL schema. Check that first. Also see my blog post[^] on this topic.

          'Howard

          U 1 Reply Last reply
          0
          • H Howard Richards

            If the GUID is set using a SQL DEFAULT it won't work with LINQ to SQL unless you flag the primary key as IsDbGenerated = true in the LINQ to SQL schema. Check that first. Also see my blog post[^] on this topic.

            'Howard

            U Offline
            U Offline
            User 3055467
            wrote on last edited by
            #5

            I've set IsDBGenerated=true in layout, designer.cs files but now i'm getting error The primary key column of type 'UniqueIdentifier' cannot be generated by the server.

            H S 2 Replies Last reply
            0
            • U User 3055467

              I've set IsDBGenerated=true in layout, designer.cs files but now i'm getting error The primary key column of type 'UniqueIdentifier' cannot be generated by the server.

              H Offline
              H Offline
              Howard Richards
              wrote on last edited by
              #6

              Ah.. looks like GUID autogenerated keys are not very well supported. In that case you need to take it off again and follow the alternative route as discussed in my blog post: set the GUID in your code.

              Private Sub InsertXXX(instance as XXX)
              'generate primary key
              instance.GUID = GUID.NewGuid()
              ExecuteDynamicInsert(instance)
              End Sub

              Replace the two XXX bits with the name of your table

              'Howard

              1 Reply Last reply
              0
              • U User 3055467

                I've set IsDBGenerated=true in layout, designer.cs files but now i'm getting error The primary key column of type 'UniqueIdentifier' cannot be generated by the server.

                S Offline
                S Offline
                s kleinschmidt
                wrote on last edited by
                #7

                Is your GUID column set as the "row guid column"? If not, do so, then the GUID will be auto generated and LINQToSQL will function with IsDBGenerated = true.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups