Using MongoDB with Entity Framework Core in DDD: Generating ObjectId for Entity Ids while Maintaining Abstraction

I don’t have the answer to your question, but if you talk about DDD, do not use the term ‘entity’ to refer to an Aggregate Root (the only one that needs a repository). This differentiation will help many newbies avoid confusion between Aggregate Roots and Entities (and Value Objects, but you didn’t mention those).

I have a similar issue, but I am using GUID. My problem is more around the GuidRepresentation.

When I use the collection directly, I can read without any issues, but if I use the DbContext, I get the following message:

MongoDB.Bson.BsonSerializationException: GuidSerializer cannot serialize a Guid when GuidRepresentation is Unspecified.

It seems that the database created internally by the DbContext extension from Mongo is not aware of the serializers’ global configuration.

I will create a post about it in a moment, but I wanted to share it here in case you encounter it.

Thanks!