Skip to content

Instantly share code, notes, and snippets.

@rowanmiller
Created February 11, 2016 22:31
Show Gist options
  • Save rowanmiller/7839768db7fa2cd29b20 to your computer and use it in GitHub Desktop.
Save rowanmiller/7839768db7fa2cd29b20 to your computer and use it in GitHub Desktop.
EF7 | Use DbSet property names as table names
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var dbSets = GetType().GetProperties()
.Where(p => p.PropertyType.Name == "DbSet`1")
.Select(p => new
{
PropertyName = p.Name,
EntityType = p.PropertyType.GenericTypeArguments.Single()
})
.ToArray();
foreach (var type in modelBuilder.Model.GetEntityTypes())
{
var dbset = dbSets.SingleOrDefault(s => s.EntityType == type.ClrType);
if(dbset != null)
{
type.Relational().TableName = dbset.PropertyName;
}
}
// Other configuration code...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment