dotnet/EntityFramework.Docs

Include example how to RETRIEVE a list of those key fields (whether 1 key or more)

jrichview opened this issue · 1 comments

Type of issue

Missing information

Description

[Enter feedback here]
The page would be much more informative if it included examples of how to retrieve a list of the properties (or just property) that are part of the primary key.

As a generalization, it seems bad practice to illustrate how to set something but not how to get it. But there are definite use cases for this on this particular page.

For example, you want to insert a new row, but you want to pre-validate that there's not going to be a primary key violation. You might retrieve a list of PK values and write code to check that your new entity won't throw this error when you try to insert. If it's a single-value key, not too hard. But if you want a general piece of code that will retrieve a primary key value from an entity regardless of the number of properties involved in the key.

Thanks!

Page URL

https://learn.microsoft.com/en-us/ef/core/modeling/keys?tabs=data-annotations

Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/modeling/keys.md

Document Version Independent Id

f1fba5b2-eb2e-da0a-fce0-d492855c1777

Article author

@AndriySvyryd

roji commented

@jrichview this section is about configuration the EF model, which is something that most applications have to do. In contrast, reading the EF model is something that generally shouldn't be needed - normal applications should be written simply assuming the model is configured the way it is.

Querying the model may be needed for certain dynamic pieces of code, which as you mention may want to get the primary key property of any entity type. These are very rare use cases, and I definitely don't believe we should make these entry-level more complex. The APIs do exist, but they almost never need to be used - so I don't think this belongs here.