Problem with Unique Identifier column in SQl Server compact edition
-
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
-
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
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
-
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
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.
-
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.
-
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.
-
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.
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 SubReplace the two XXX bits with the name of your table
'Howard
-
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.
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.