1
Vote

DbUpdateConcurrencyException on insert of non-key identity value

description

EF 5.x or 6.x
(Database First)

I am getting a DbUpdateConcurrencyException on DBContext.SaveChanges() after inserting a new (Detached) object with an identity column which is not part of the primary key .

Easy to replicate:
1) Create table with identity column that is not part of the primary key.
CREATE TABLE [dbo].[TestIdentityInsertTable](
    [PriColumn] [int] NOT NULL,
    [StringColumn] [nvarchar](12) NULL,
    __[NonPriIdentityColumn] [int] IDENTITY(1,1) NOT NULL,__
 CONSTRAINT [PK_TestIdentityInsertTable] PRIMARY KEY CLUSTERED 
(
    [PriColumn] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
2) Add/Update EF model and ensure StoreGeneratedPattern is Identity on the identity column
3) Test code:
                        using (TestInsertEntities dbContext = new TestInsertEntities())
            {
                TestIdentityInsertTable newRecord = new TestIdentityInsertTable();
                newRecord.PriColumn = 1;
                newRecord.StringColumn = "TestRecord";

                dbContext.SaveChanges();
            }
Exception is raised at SaveChanges();

comments

TitanApps wrote Oct 1, 2015 at 12:41 AM

Apologies, just realized i have posted this issue to the Contrib project not the base EF.
Mods please delete/close issue for me.