1

I'm using the code-first approach and because of that I do not have the .edmx file so I cannot change the StoreGeneratedPattern attribute. How do I then make EF auto-increment the IDs? (right now all IDs are zeros by default)

2
  • what you are looking for is DatabaseGeneratedAttribute with DatabaseGeneratedOption as Identity. Commented Sep 26, 2015 at 14:43
  • When I use DatabaseGeneratedOption.None it doesn't auto-increment, but when I use DatabaseGeneratedOption.Identity it doesn't even generate the Id (leaves it null) Commented Sep 26, 2015 at 16:07

2 Answers 2

1

A primary key with auto-increment (default behavior) is generated if any of the following holds true:

  • The class contains a property named "Id"
  • The class contains a property named $className + "Id"
  • The property of interest is annotated with KeyAttribute

Might it be that your property is named "ID" instead of "Id" without the Key attribute?

Sign up to request clarification or add additional context in comments.

2 Comments

I use the Key attribute and the property is named Id
What is the type of the property? int or something else?
1

There are three ways to specify keys using EF code first:

  • Use built-in code conventions (his first two bullets)
  • Use the KeyAttribute data annotation directly on the property/field
  • Use the Fluent API to specify the key field

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.