I'm trying to insert a record into a table using Linq, but get scary I can not add an object with an already used key Error
'If the same data exists for the same patient in a record less that 21 days old then drop it Dim RecordLookup As Integer = 0 RecordLookup = (From rc In CDEvodb.RISKCHANGEs _ Where rc.NHI = tmpNHI And _ rc.RECDATE > Date.Now.AddDays(-21) And _ rc.BPSYS = Convert.ToDecimal(Drow.Item("BPSYS")) And _ rc.CHOL = Convert.ToDecimal(Drow.Item("CHOL")) And _ rc.HDL = Convert.ToDecimal(Drow.Item("HDL"))).Count() If (RecordLookup = 0) Then Dim riskchange As New RISKCHANGE riskchange.NHI = Drow.Item("NHI") riskchange.RECDATE = Date.Now.Date() riskchange.RISK = CalculatedRisk riskchange.BPSYS = Drow.Item("BPSYS") riskchange.CHOL = Drow.Item("CHOL") riskchange.HDL = Drow.Item("HDL") Try CDEvodb.RISKCHANGEs.InsertOnSubmit(riskchange) Catch ex As Exception myLogging.OutputError("<" & DateTime.Now.ToString & "> " & "Error - creating risk change record in dataset for patient " & Drow.Item("NHI").ToString() & " - " & ex.Message) End Try End If
I basically look at the table for the corresponding record (not including the Identity field), which is less than 21 days. If I do not find it, I instantiate the string and install it.
The SubmitChanges function calls a few lines down.
Drow is a DataRow from a dataset that was previously populated using the SQLClient connection (the reason I have not fully converted to Linq yet, but is now just performing new functions).
Greetings in advance.
this is creating a script for the table:
USE [CDEvolution]
GO
/ ****** Object: Table [dbo]. [RISKCHANGES] script Date: 05/13/2009 14:40:15 ****** /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo]. [RISKCHANGES] (
[NHI] [varchar](7) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [RECDATE] [datetime] NOT NULL, [RISK] [numeric](15, 0) NOT NULL, [BPSYS] [numeric](15, 0) NOT NULL, [CHOL] [numeric](15, 1) NOT NULL, [HDL] [numeric](15, 1) NOT NULL, [POSTED] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [IDENTITY] [uniqueidentifier] NOT NULL CONSTRAINT [DF_RISKCHANGES_IDENTITY]
DEFAULT (newid ()),
CONSTRAINT [PK_RISKCHANGES] PRIMARY KEYBOARD
(
[IDENTITY] ASC
) WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF