Ok, so i am using entity framework with dot net core and code first migrations. This isn't a problem as such, i just wondered if anyone had come across a better way of doing this.
Currently i have many entity type configurations like so
public class ExampleEntityConfiguration : IEntityTypeConfiguration<ExampleEntity>
{
public void Configure(EntityTypeBuilder<ExampleEntity> builder)
{
builder.Property(p => p.Id).ValueGeneratedNever();
// more options here
}
}
and i register them in my dbcontext like so
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new ExampleEntityConfiguration());
// lot's more configurations here
}
has anyone come across or know of a way to register all the IEntityTypeConfiguration
interfaces?
It just seems like a lot of repetitive code that could be solved by getting a list of the configurations, looping over them and applying in the context. I just don't know where to start with getting a list of IEntityTypeConfiguration
classes that exist in a particular namespace.
Any help/suggestions would be great.
IEntityTypeConfiguration
. Something like:AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes().Where(t => t.IsClass && typeof(IEntityTypeConfiguration<>).IsAssignableFrom(t)))