Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save lprichar/c0a6b2d428062404f1811204b2c7a9a0 to your computer and use it in GitHub Desktop.

Select an option

Save lprichar/c0a6b2d428062404f1811204b2c7a9a0 to your computer and use it in GitHub Desktop.
Reflection code to configure all EF entities by convention
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