Skip to content

Instantly share code, notes, and snippets.

@ahmadalli
Last active January 27, 2017 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadalli/a1103dc0b27e8f01287b148fd9d4c426 to your computer and use it in GitHub Desktop.
Save ahmadalli/a1103dc0b27e8f01287b148fd9d4c426 to your computer and use it in GitHub Desktop.
Dynamically load Entity Configurations in EF CodeFirst
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var typesToRegister = Assembly.GetAssembly(typeof(Context)).GetTypes()
.Where(type => type.Namespace != null)
.Where(type => type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)).ToList();
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
base.OnModelCreating(modelBuilder);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment