I'm using EF Core 2 and I'm trying to add an entity dynamically to the the model. The scenario is:
The user creates an entity with custom properties in a web app
I create at runtime a class that maps this entity (see How to emit a Type in .NET Core)
I add the created class to the model in the
OnModelCreating method of the db context (see https://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/)
Everything works, except when I want to write to the db a new instance of the newly created class in the same request (API call) where I created the new entity.
This is because the
DbContext is scoped to the request (not transient), so the
DbContext called the
OnModelCreating method before I had the chance to create the new entity.
This is the error that I get:
The entity type 'People' was not found. Ensure that the entity type has been added to the model.
Any suggestions on how to accomplish that? I don't think it's possible to call
OnModelCreating on demand and I can't find a way to access the
ModelBuilder of the context to call it's
You would also need a new DbContext subtype, as OnModelCreating only runs once per DbContext type per AppDomain.