Created
October 25, 2020 15:11
-
-
Save lprichar/c0a6b2d428062404f1811204b2c7a9a0 to your computer and use it in GitHub Desktop.
Reflection code to configure all EF entities by convention
This file contains hidden or 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
| public static class LeesStoreDbContextModelCreatingExtensions | |
| { | |
| public static void ConfigureLeesStore(this ModelBuilder builder) | |
| { | |
| var allDbSets = typeof(LeesStoreDbContext).GetProperties() | |
| .Where(p => p.PropertyType.Name == "DbSet`1") | |
| .Select(p => new | |
| { | |
| Type = p.PropertyType.GetGenericArguments()[0], | |
| p.Name | |
| }); | |
| foreach (var property in allDbSets) | |
| { | |
| var type = property.Type; | |
| builder.Entity(type, i => | |
| { | |
| i.ToTable(LeesStoreConsts.DbTablePrefix + property.Name, LeesStoreConsts.DbSchema); | |
| i.ConfigureByConvention(); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment