SkeetJon
SkeetJon

Reputation: 1491

Entity Framework & Primary Key

I am using Entity Framework and am trying to create a new record in the database using the static Create method on the entity.

Specifically there is a table in the DB called Alleles, but the CreateAllele method asks for a value for Id, which is the PK for this table.

Should I really need to provide this? Isnt it up to the DB to auto increment the PK?

Here's my table represented by EF

Here's the intelli-sense ask for a PK value

Upvotes: 0

Views: 678

Answers (2)

Gert Arnold
Gert Arnold

Reputation: 109253

If Id is an IDENTITY column you can just provide a value (e.g. -1) and EF will replace it by the generated key when the record id committed.

Upvotes: 1

Emmie Lewis-Briggman
Emmie Lewis-Briggman

Reputation: 855

You can use SQL Management studio in order to change the identity value for a column or you can include the identity property in your create script. Refresh your entity diagram and it should be good to go.

IDENTITY is a property you could set on a table. You could only set this property on one column in the entire table. SQL Server will take care of incrementing this column based on the seed you gave while creating the property.

IDENTITY [ ( seed , increment ) ]

Seed — Is the value that is used for the very first row loaded into the table.

Increment — Is the incremental value that is added to the identity value of the previous row that was loaded.

Default values – (1,1)

How to create an identity column while creating a new table?

CREATE TABLE tblEmployees
(
  ID int IDENTITY(1,1),
  Name varchar (200),
  SSN varchar(10)
)

Upvotes: 0

Related Questions