I鈥檓 trying to learn how Entity Framework core uses C# classes to create the DbContext model. I assume it discovers types based on DbSets then calls the same model builder that is passed to OnSchemaCreating, I just can鈥檛 find the code/logic in the repository that does this (or perhaps there is another approach). Can anyone explain how this is done or point to the source code that does this?
Asked on SO but no luck:
https://stackoverflow.com/questions/52032276/how-does-entity-framework-core-implement-code-first
@smitpatel thank you! Just to confirm I'm on the right path, from what I understand the ModelCustomizer is pretty basic - it simply adds entity types from the DbContext, then conventions take care of the rest?
@craigsmitham ModelCustomizer in above code is service registered for IModelCustomizer interface. Look for service registration of that type in codebase. At core level we register ModelCustomizer class & at relational level we register RelationalModelCustomizer. You can search for those 2 classes in codebase to see what exactly they are doing and how they figure out whole model. As you suspect, these are entry points for some entityTypes and certain other things. Rest is conventions which adds connected entities and configure facets using reflection.
Thanks again @smitpatel ! Very helpful.
Most helpful comment
https://github.com/aspnet/EntityFrameworkCore/blob/93fb3f0054892143e8b4a8b6520497dd2476394f/src/EFCore/Infrastructure/ModelSource.cs#L70-L91