Last active
January 27, 2017 22:02
-
-
Save ahmadalli/a1103dc0b27e8f01287b148fd9d4c426 to your computer and use it in GitHub Desktop.
Dynamically load Entity Configurations in EF CodeFirst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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