Created
February 11, 2016 22:31
-
-
Save rowanmiller/7839768db7fa2cd29b20 to your computer and use it in GitHub Desktop.
EF7 | Use DbSet property names as table names
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(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