0

I'm trying to add records to a local database using Visual Studio 2015 and linq to sql commands, but I get an error IDENTITY_INSERT is set to OFF.

I'm not inserting a value for the primary key and Identity Specification is set to True. This is the generated script file:

CREATE TABLE [dbo].[MONTE_CARLO] (
    [PK]        INT             IDENTITY (1, 1) NOT NULL,
    [ID]        INT             NOT NULL,
    [BOOK_COMP] NCHAR (10)      NOT NULL,
    [GREEK]     NCHAR (10)      NOT NULL,
    [DP1]       DECIMAL (18, 6) NOT NULL,
    [DP2]       DECIMAL (18, 6) NOT NULL,
    [DP3]       DECIMAL (18, 6) NOT NULL,
    [DP4]       DECIMAL (18, 6) NOT NULL,
    [DP5]       DECIMAL (18, 6) NOT NULL,
    [DP6]       DECIMAL (18, 6) NOT NULL,
    [DP7]       DECIMAL (18, 6) NOT NULL,
    [DP8]       DECIMAL (18, 6) NOT NULL,
    [DP9]       DECIMAL (18, 6) NOT NULL,
    [DP10]      DECIMAL (18, 6) NOT NULL,
    [DP11]      DECIMAL (18, 6) NOT NULL,
    [DP12]      DECIMAL (18, 6) NOT NULL,
    [DP13]      DECIMAL (18, 6) NOT NULL,
    [DP14]      DECIMAL (18, 6) NOT NULL,
    [DP15]      DECIMAL (18, 6) NOT NULL,
    [DP16]      DECIMAL (18, 6) NOT NULL,
    [DP17]      DECIMAL (18, 6) NOT NULL,
    [DP18]      DECIMAL (18, 6) NOT NULL,
    [DP19]      DECIMAL (18, 6) NOT NULL,
    [DP20]      DECIMAL (18, 6) NOT NULL,
    [DP21]      DECIMAL (18, 6) NOT NULL,
    CONSTRAINT [PK_MONTE_CARLO] PRIMARY KEY CLUSTERED ([PK] ASC)
);

This is the code to update the datatable. I'm passing a dictionary (dct)to the function which handles the updating.

Dim mcQuery = From updtMC In DATA.MONTE_CARLOs        'ID exists so update data
                          Where updtMC.ID = CStr(dct("ID")) And updtMC.BOOK_COMP = CStr(dct("BOOK_COMP")) And updtMC.GREEK = CStr(dct("GREEK"))
                          Select updtMC
            For Each updtMC As MONTE_CARLO In mcQuery

                updtMC.ID = dct("ID")
                updtMC.BOOK_COMP = dct("BOOK_COMP")
                updtMC.GREEK = dct("GREEK")
                updtMC.DP1 = dct("DP1")
                updtMC.DP2 = dct("DP2")
                updtMC.DP3 = dct("DP3")
                updtMC.DP4 = dct("DP4")
                updtMC.DP5 = dct("DP5")
                updtMC.DP6 = dct("DP6")
                updtMC.DP7 = dct("DP7")
                updtMC.DP8 = dct("DP8")
                updtMC.DP9 = dct("DP9")
                updtMC.DP10 = dct("DP10")
                updtMC.DP11 = dct("DP11")
                updtMC.DP12 = dct("DP12")
                updtMC.DP13 = dct("DP13")
                updtMC.DP14 = dct("DP14")
                updtMC.DP15 = dct("DP15")
                updtMC.DP16 = dct("DP16")
                updtMC.DP17 = dct("DP17")
                updtMC.DP18 = dct("DP18")
                updtMC.DP19 = dct("DP19")
                updtMC.DP20 = dct("DP20")
                updtMC.DP21 = dct("DP21")
            Next
        End If

        DATA.SubmitChanges()

Monte_Carlo settings

linq to sql files for Monte_Carlo:

<Global.System.Data.Linq.Mapping.TableAttribute(Name:="dbo.MONTE_CARLO")>  _
Partial Public Class MONTE_CARLO
    Implements System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged

    Private Shared emptyChangingEventArgs As PropertyChangingEventArgs = New PropertyChangingEventArgs(String.Empty)

    Private _PK As Integer

    Private _ID As Integer

    Private _BOOK_COMP As String

    Private _GREEK As String

    Private _DP1 As Decimal

    Private _DP2 As Decimal

    Private _DP3 As Decimal

    Private _DP4 As Decimal

    Private _DP5 As Decimal

    Private _DP6 As Decimal

    Private _DP7 As Decimal

    Private _DP8 As Decimal

    Private _DP9 As Decimal

    Private _DP10 As Decimal

    Private _DP11 As Decimal

    Private _DP12 As Decimal

    Private _DP13 As Decimal

    Private _DP14 As Decimal

    Private _DP15 As Decimal

    Private _DP16 As Decimal

    Private _DP17 As Decimal

    Private _DP18 As Decimal

    Private _DP19 As Decimal

    Private _DP20 As Decimal

    Private _DP21 As Decimal

    Private _BOOK As EntityRef(Of BOOK)
Zeus
  • 1,496
  • 2
  • 24
  • 53
  • shoq the linq where you add the records – Alex B. Jan 29 '16 at 07:48
  • Tks for the response...I've edited the question to show the code to add records. – Zeus Jan 29 '16 at 07:52
  • Can you also show class MONTE_CARLOs please? – Alex B. Jan 29 '16 at 08:21
  • ok but I'm not sure what you mean, where will I find the class for the datatable ``MONTE_CARLO``? – Zeus Jan 29 '16 at 08:26
  • right-click on MONTE_CARLO -> Go to definition – Alex B. Jan 29 '16 at 08:35
  • Alex I've added a screenshot of the datatable settings to the question. When I right clicked on the table I could only see ``open table definition``. Let me know if you need something else. – Zeus Jan 29 '16 at 09:18
  • No I mean the LinqToSql class. There must be a class in your `lnqPOLARIS.dbml` file which represents the MONTE_CARLO table. I need the code behind to help you. There should be an property like ` _ Public Property PK() As Integer` – Alex B. Jan 29 '16 at 10:05
  • ok i know what you mean, I just can't find it. If I right click on ``lnqPolaris.dbml`` in solution explorer I can open a window to view code, but there is no code there like what you are looking for. Where else should I look? – Zeus Jan 29 '16 at 10:37
  • In Project Explorer click `Show all files` -> Expand `lnqPOLARIS.dbml` -> expand `lnqPOLARIS.designer.vb` -> double-click `MONTE_CARLO` – Alex B. Jan 29 '16 at 11:07
  • tks I found it and added the code to the bottom of the question. – Zeus Jan 29 '16 at 12:37
  • Unless you *specify* a different key, Linq to SQL assumes the key is `ID`. Check your mapping, not the database. Far better is to use ID as your primary key though - *anyone* looking at the schema will assume a column named ID is the actual .... identifier for the entity. Why do you define a separate column as Identity anyway? – Panagiotis Kanavos Jan 29 '16 at 12:52
  • tks, to you and shukri, I'll try changing ID to another name and see if it helps. ID maps to another table and to keep the code simple for now I made ID another data column, not the primary key. – Zeus Jan 29 '16 at 13:26

1 Answers1

2

Based on naming conventions in entity framework columns with name Id are considered as Id, probably you are having this problem. Try to change the name of field ID.

Shukri Gashi
  • 535
  • 2
  • 10
  • Actually, the OP should rename PK to ID. Anyone looking at the schema will assume *this* is the unique identifier, not another field named PK – Panagiotis Kanavos Jan 29 '16 at 12:54