I am using C# Linq to have interaction by having an MSSQL database. I've got a problem where I sometime obtain the error message:
"Breach of PRIMARY KEY constraint 'PK_clustered_key_name'. Cannot place duplicate type in object 'dbo.table_name'. The statement has been ended."
After I consider adding information towards the database. Once the error happens I actually do an sql query using the values I consider adding, within the MSSQL Management Studio there does not appear to become an entry within the table.
So my real question is if anybody includes a clue by what the problem may be here. Some more information follows.
The code I run (the mistake happens at
SaveChanges) may be the following:
table_name table_name_instance = new table_name(); ... fill in values of fps ... context.table_name.AddObject(fps); context.SaveChanges();
I have also added a cheque if values of col1, col2, col3, col3 already appear in database and so i definetly will not add duplicate records. Since I am unsure when the add is postponed and also the check return that the duplicate entry already exist I have saved added values inside a local container too just to be certain, however i get the mistake when calling SaveChanges().
The phrase my table using the clustered key that the mistake is reported follows. Col1 through col4 is Guid:s which are Foreign secrets with other tables.
CREATE TABLE [dbo].[table_name]( [col1] [uniqueidentifier] NOT NULL, [col2] [uniqueidentifier] NOT NULL, [col3] [uniqueidentifier] NOT NULL, [col4] [uniqueidentifier] NOT NULL CONSTRAINT [PK_forbandsscenariokrav] PRIMARY KEY CLUSTERED ( [col1] ASC, [col2] ASC, [col3] ASC, [col4] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
I have seen many similar questions but haven't found a solution that appear to use to my situation.
Information, I am using MSSQL server 2008 R2 under Win 7 64-bit in addition to:
- Microsoft SQL Server Management Studio 10.50.1600.1
- Microsoft Data Access Components (MDAC) 6.1.7601.17514
- Microsoft MSXML 3. 5. 6.
- Microsoft Ie 8..7601.17514
- Microsoft .Internet Framework 2..50727.5448
- Operating-system 6.1.7601
After I rather make use of the Create function such as this:
table_name table_name_instance = Createtable_name(col_val, col2_val,...); context.table_name.AddObject(fps); context.SaveChanges();
It appears to operate all right. Does that provide any hint on why my first solution with new wasn't working despite the fact that I completed every column value, not accepting null having a value?
Decide if this SQL statement creates your SQL Server
INSERT INTO [dbo].[table_name] ([col1] ,[col2] ,[col3] ,[col4]) VALUES (NEWID(), NEWID(), NEWID(), NEWID()) GO
Whether it does then which means the Guid you're creating out of your application are copying. Before placing data inside your database check if the primary key you need to place is available within the table. You need to do this for every column as every column is really a PK.