I have an entity (several, actually) that I want to have assigned a key value based on custom logic when they’re created. I know I could do it in the entity constructor, but ideally I’d like to do it as part of the data context logic for when a new entity is created.
I found how to turn off autogeneration, but what I’d rather do is replace auto generation w/ my own logic, ideally handled by the C# code (I”ve seen techniques for doing it via stored procs also, that I’d rather not use).
Is this even possible to do centrally?
Autogeneration happens on the server side and is not done by the EF. This is a setting on a table key column. So EF does not generate any keys – if autogeneration is turned on this is the database that generates keys, if autogeneration is off this is the user that is responsible for generating keys. If you don’t want to generate keys when saving changes you may want to override SaveChanges and generate keys for all newly added entities.