Visual Studio - IDENTITY_INSERT设置为OFF

时间:2016-01-29 07:41:19

标签: vb.net visual-studio linq-to-sql datatable submitchanges

我尝试使用Visual Studio 2015和linq to sql命令将记录添加到本地数据库,但收到错误IDENTITY_INSERT is set to OFF

我没有为主键插入值,Identity Specification设置为True。 这是生成的脚本文件:

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)
);

这是更新数据表的代码。我将字典(dct)传递给处理更新的函数。

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 Monte_Carlo的sql文件:

<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)

1 个答案:

答案 0 :(得分:2)

根据实体框架中的命名约定,名称为Id的列被视为Id,可能是您遇到此问题。尝试更改字段ID的名称。