I'm writing a library that's going to provide new APIs around the DbContext
class in Entity Framework Core 5+. I already have a version of these new APIs working, but it requires manual intervention in the final DbContext
implementation, e.g.:
// Code in the library.
public static class AwesomeExtensions
{
public static ModelBuilder AddAwesomeExtensionsSupportingEntities(this ModelBuilder modelBuilder)
{
// Set up custom entities I need to make this work.
return modelBuilder;
}
}
// Code somewhere else.
public class MyBusinessDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// I would like to avoid doing this here.
modelBuilder.AddAwesomeExtensionsSupportingEntities();
// Business as usual (database entities).
}
}
After an extended search I haven't found an extension point in the EF Core API that allows me to do this in a non-intrusive way.
This is what I have found so far:
- CustomDbContext class: I could inherit from
DbContext
and override theOnModelCreating
method, but this is not superior to what I'm doing right now. DbContextOptionsBuilder.UseModel
: I thought this may be something I could use but adds too much complexity. By using this API theOnModelCreating
method won't be called by the framework.IEntityTypeConfiguration<TEntity>
: I was rooting for this one but it also requires you to have access to theModelBuilder
instance, then you can use theModelBuilder.ApplyConfigurationsFromAssembly
method.
Ideally, I would like to do this via the DbContextOptionsBuilder
object provided when registering the DbContext
dependency, e.g.:
// Code in some application.
public void AddServices(IServiceCollection services)
{
services.AddDbContext<MyBusinessDbContext>(options =>
{
// The ideal solution.
options.UseAwesomeExtensions();
});
}
If I could only intercept the instance of the ModelBuilder
just before it is provided to the OnModelCreating
method, in a way that does not requires the modification of the DbContext
implementation, that would help me.
Any ideas are welcome.
Thank you.