I have created a database where I use uniqueidentifier as the primary key. I'm trying to run a parametrized query that in the end looks like this. (insert into someTable (uniqueID) Values('76c14693-5475-4224-ba94-7d30c919ac59') (uniqueID is my PK). This insert statement executes without issues when I run it in SQL Server Management Studio, however, it fails when I try to run it from my code.
Error: Cannot insert explicit value for identity column in table 'someTable' when IDENTITY_INSERT is set to OFF.
I have looked this up and there are a lot of suggestions on turning ON the IDENTITY_INSERT but they all use stored procedures. I need to be able to do this using a parametrized query. Or I need a way to get the newly created guid in the database. This seems like something everyone would need at some point. Are there any simple solutions?
PS. I need the ID because I need to add records to other tables using that ID.
EDIT:
Here is the Structure of the table in question
CREATE TABLE [dbo].[someTable]
(
[NewsID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_someTable_NewsID] DEFAULT (newid()),
[ShortTitle] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DateCreated] [datetime] NULL CONSTRAINT [DF_someTable_DateCreated] DEFAULT (getdate()),
CONSTRAINT [PK_cmc_News_1] PRIMARY KEY CLUSTERED (
[NewsID] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF
) ON [PRIMARY]
) ON [PRIMARY]
Thanks in advance!
Thanks to you guys my issue is now resolved. I was connecting to the wrong database that unfortunately had the same table name but used an INT Identity field. So, that is why I was getting that error. Sorry everyone. I just wasted your time here, but I would not have figured this out if it wasn't for some of the posts. If it helps you feel any better I just wasted my whole morning on this stupid issue. Thanks again!