Cannot add records to MSSQL table where ket column has identity spec and table contains row with 0 in the identity column

Whilst building a simple CRUD app over some MSSQL tables I noticed an issue where a new record could not be added. After a bit of digging it appears that if the table I'm trying to add the new record to has an INDENTITY spec column (which is also the primary key in my scenario) AND there is a record present with a 0 (zero) in this column, then the insert fails.

It appears that 0 is always passed as the identity column value which causes the existingItem to be found prior to attempting the insert.

At first I thought it might be because the identity spec was INDENTITY(1,1) and that the zero value was added using IDENTITY_INSERT ON as some point in the past, but I created a new table with IDENTITY(0,1) and the same thing happens.

Using the following code to create and populate the database table

CREATE TABLE dbo.MyTestTable(
	[ProductID] [int] IDENTITY(0,1) NOT NULL,
	[ProductDesc] [nvarchar](75) NOT NULL,
 CONSTRAINT [PK_MyTestTable] PRIMARY KEY CLUSTERED 
(
	[ProductID] 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

INSERT INTO dbo.MyTestTable(ProductDesc) VALUES 
('First'), ('Second'), ('Third')

Give us records with ProductID of 0, 1 and 2

I then created an app in Blazor Studio (updated today) and used defaults for all settings/values.

I created CRUD pages, again using defaults.

When trying to add a new record I get the message "Cannot Save myTestTable"

In the myDatabaseService.cs file, when the following code runs

            var existingItem = Context.MyTestTables
                              .Where(i => i.ProductID == mytesttable.ProductID)
                              .FirstOrDefault();

the value of mytesttable.ProductID is 0

and therefore existingItem is not null.

I'm not sure if this is an EntityFramework issue or a Blazor Studio issue but would be grateful for any advice.

Hello Eggy1968,

The issue is Entity Framework. This is what happens when we hit save. You can use sql profiler to see this.

The framework first test to see if a record exists using 0 as you supected.

exec sp_executesql N'SELECT [m].[ProductID], [m].[ProductDesc]
FROM [dbo].[MyTestTable] AS [m]
ORDER BY (SELECT 1)
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY',N'@__p_0 int,@__p_1 int',@__p_0=0,@__p_1=10

Go to the following article and scroll down to Example. There you will see the answer.

Enumerable.FirstOrDefault Method

I would recommend not using 0 as a starting number in a table. Or you could edit the generated code to what is a work around that you can find in that article below the example.

Hi Kurt,

Thanks for confirming. Unfortunately 0 has been used as a reserved value in most of our 'dimension' tables for over 20 years so changing it would be a huge job. I'll have to manually edit the code. Each table of this type also has a unique index on the description column so I'll edit the code to use that instead.

As I've never done this before, is there a way to prevent the edited code from being overwritten? if so could you point me towards any articles that describe this process.

Many thanks.
Al.

Hello Al,

I do not know of any articles or where to point you. Please send me an email with your contact info and I will reach back out to you. I could setup a zoom meeting with you and go over what you need to do. kurtkluth@me.com

Kurt