LINQ to SQL insert primary key index?

If I just define my ID column as IsPrimaryKey = true and IsDbGenerated = true everything works just fine: ColumnAttribute(Storage="_ID", AutoSync=AutoSync. OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true) public int ID {....} Then, in code - when I create a new entity, I just assign the entity an arbitrary ID I typically tend to use negative numbers: MyEntity entity = new MyEntity { ID = -55 }; MyContext.MyEntities. InsertOnSubmit(entity); MyContext.CommitChanges() After the call to CommitChanges() the new entities are stored in the database table, and the actual, real ID values ( INT IDENTITY ) are being set and reflected on my entity object - i.e.

After the call, I get back the real ID that the database has given my new entity So my question is: where is your problem then? It seems to work just fine.

If I just define my ID column as IsPrimaryKey = true and IsDbGenerated = true, everything works just fine: ColumnAttribute(Storage="_ID", AutoSync=AutoSync. OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true) public int ID {....} Then, in code - when I create a new entity, I just assign the entity an arbitrary ID - I typically tend to use negative numbers: MyEntity entity = new MyEntity { ID = -55 }; MyContext.MyEntities. InsertOnSubmit(entity); MyContext.CommitChanges(); After the call to .CommitChanges(), the new entities are stored in the database table, and the actual, real ID values (INT IDENTITY) are being set and reflected on my entity object - i.e.

After the call, I get back the real ID that the database has given my new entity. So my question is: where is your problem then? It seems to work just fine....

I've added my full code to the top :) – Baconbeastnz Apr 12 at 23:16 cheers! & sorry for late response – Baconbeastnz Apr 15 at 4:03.

The creation is ok for the first register but the primary key is 0!?! Violation of PRIMARY KEY constraint 'PK_Messages'. Cannot insert duplicate key in object 'dbo.

The statement has been terminated. I do not understand why the primary key is forced to 0 into a InsertOnSubmit(). It should be automatically incremented.

The view is generated using scaffolding and doesn't submit anything for the Id field.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions