Let's assume we have an application written using DDD approach. Also, entities and aggregate roots are stored in the database using an incremental identity integer as primary key. As entities are identified by an unique Id, the Id must be know at the creation.
So in this case, how the id must be generated? I see different possibilities:
The entity is created with a null or default id value and the id will be created only when the entity persisted in the db
Querying each time the DB to get the next Id to be used
Using another type of Id (ex:GUID) that could be instantiated without db round trips.
All of these possibilities have their own drawback. In the first one the entity can't be identified during the running of the app and having dummy or null Ids doesn't seems a good practice. The second it's heavy and that force us to pass the service calling the next id generation around the domain. The third one could works, but that mean changing all our db primary keys, which we may have chosen for good reasons.
Maybe, my first assumption is wrong and Entities or Aggregate root should never be created on the fly and always provided by repositories? Thanks