I am not sure if this is possible, but I have started a new project and I am trying to clean up the way I work with the DbContext. I am trying to do this by
Well, you need some way to point towards the ID / key property.
Some ideas to explore:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
) to make things easierGetAsync
method (obviously the code wouldn't be as simple as it is now)Of all of these solutions, I'd go for the first or second one as those seem to be most "clear". If you decide to go for reflection, then I'd go for the second one (where you have a key-defining attribute) - but this still forces you to go through all your entities and make changes. If you have a LOT of entities, then perhaps the text-based property search might be the fastest - but obviously any entity which doesn't have a property named "Id" will cause issues.
Obviously YMMV depending on what you actually want to accomplish. Note that you can try and combine the reflection solutions (find by name, if not found search by key attribute).
Also remember that while reflection is a powerful tool, it has a performance impact.