S
sqlguy
Guest
I am creating a sample EF app. I am using SQL Server 14 and have created three tables. Each of the tables has as a Primary Key an identity column. The following is the first code in the sample (I have other much more complicated apps that use EF with no problems).
Dim esde As New EntitySampleDatabaseEntities1
For i As Integer = 0 To 9
Dim cust As New Customer
cust.CustomerName = "Customer " & i.ToString
esde.Customers.Add(cust)
Next
Dim cnt = CType(esde, IObjectContextAdapter).ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added).Count
Dim savcnt = esde.SaveChanges()
The create/drop for the Customer table is :
USE [EntitySampleDatabase]
GO
/****** Object: Table [dbo].[Customer] Script Date: 2018-08-23 1:27:00 PM ******/
DROP TABLE [dbo].[Customer]
GO
/****** Object: Table [dbo].[Customer] Script Date: 2018-08-23 1:27:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Customer](
[CustomerId] [int] IDENTITY(1,1) NOT NULL,
[CustomerName] [varchar](255) NOT NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[CustomerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
When I execute the code it fails on the SaveChanges with the following inner message:
{"Cannot insert explicit value for identity column in table 'Customer' when IDENTITY_INSERT is set to OFF."}
Lloyd Sheen
Continue reading...
Dim esde As New EntitySampleDatabaseEntities1
For i As Integer = 0 To 9
Dim cust As New Customer
cust.CustomerName = "Customer " & i.ToString
esde.Customers.Add(cust)
Next
Dim cnt = CType(esde, IObjectContextAdapter).ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.Entity.EntityState.Added).Count
Dim savcnt = esde.SaveChanges()
The create/drop for the Customer table is :
USE [EntitySampleDatabase]
GO
/****** Object: Table [dbo].[Customer] Script Date: 2018-08-23 1:27:00 PM ******/
DROP TABLE [dbo].[Customer]
GO
/****** Object: Table [dbo].[Customer] Script Date: 2018-08-23 1:27:00 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Customer](
[CustomerId] [int] IDENTITY(1,1) NOT NULL,
[CustomerName] [varchar](255) NOT NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[CustomerId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
When I execute the code it fails on the SaveChanges with the following inner message:
{"Cannot insert explicit value for identity column in table 'Customer' when IDENTITY_INSERT is set to OFF."}
Lloyd Sheen
Continue reading...