What does the `Key` Data Annotation do in database first?

900 views Asked by At

Everything I find through Google refers to Code First so I'm wondering what the Key attribute is actually doing in regards to a Database First design? I'm mainly curious because a lot of the entities contain composite keys so I've been adding the Key annotation to the respective properties, but is this really necessary? And if so, what do I gain from it?

1

There are 1 answers

2
bricelam On BEST ANSWER

If you're using the database-first workflow (i.e. you have an .edmx file in your solution). Then the Key attribute will have no effect.

However, if you're using the code-first workflow to map to an existing database, the Key attribute tells code first that that property is part of the entity's primary key.

For more info on the different workflows see this video: Visual Studio Toolbox: Entity Framework Part 1