DEV Community

Alexandru Bucur
Alexandru Bucur

Posted on

7

How to rename Asp .Net Core 2.2 Identity Tables to not have AspNet prefix in EF Core

Not sure if the code below should be considered a hack, but the easiest way of removing the AspNet prefix is to iterate through the models.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    {
        var table = entityType.Relational().TableName;
        if (table.StartsWith("AspNet")) {
            entityType.Relational().TableName = table.Substring(6);
        }
    };
}
Enter fullscreen mode Exit fullscreen mode

Found the initial code in a 2014 asp net issue and since copy paste didn't cut it, did a quick update of the method / property names.

This also allows you to replace it with whatever else you want, or add a generic table prefix.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
sgermosen profile image
Starling Germosen

On netcore >2.2 this doesnt work, so, you need to change it to this

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var tableName = entityType.GetTableName();
if (tableName.StartsWith("AspNet"))
{
entityType.SetTableName(tableName.Substring(6));
}
}

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay