question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reverse Engineering: Enable pluralization/singularization when scaffolding a model from a database

See original GitHub issue

Database scaffolding has no pluralization service and the entity models are generated mapping the exact name of database objects. My suggestion is to enable this pluralization through a DI service that could be overridden at the ef CLI like this:

dnx ef dbcontext scaffold "Server=myserver;Database=Foo;user id=me;password=pwd" EntityFramework.SqlServer --use-pluralization "MyAssembly.PlurarizationService, MyAssembly"

And on Razor templates this could be exposed as:

...
@:namespace @Model.Namespace
@:{
@:    public class  @PluralizationService.Singularize(Model.EntityType.Name)
@:    {
...

I think the default service could be the one shipped with EF6 (System.Data.Entity.Design.PluralizationServices.PluralizationService)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:25 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
gdoroncommented, Feb 12, 2016

Thanks @rowanmiller, in case someone else finds it helpful, I wrapped it in an extension method:

public static void UseDbSetNamesAsTableNames(this DbContext dbContext, ModelBuilder modelBuilder)
{

    var dbSets = dbContext.GetType().GetRuntimeProperties()
        .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;
        }

    }
}

And used it like this:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    this.UseDbSetNamesAsTableNames(builder);

But I get an error says cannot find the object “Posts”

I checked what EF is trying to execute with dnx ef migrations script 20160211201857_second -o c:\temp\rename.txt and pasted it here. EF is using the new name Posts before it was renamed, so that’s a bug.

Since it’s a just a test application I created for a lecture I will present in the office next week I don’t mind using dnx ef database 0 and start all over again. But out of curiosity what should I do if it is a real production application? fiddle with the __EFMigrationsHistory table?

Thank you very much Rowan, I really like what you did with EF 7 and in ASP.NET 5 in general!

1reaction
rowanmillercommented, Feb 11, 2016

@gdoron stick this code at the start of OnModelCreating and it will give you what you are after 😄

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...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Scaffolding (Reverse Engineering) - EF Core
Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema.
Read more >
Scaffolding a Model from a Database Schema
Reverse Engineering is one of the development workflows of EF Core. It involves scaffolding a model ( DbContext and entity types) from a...
Read more >
EF Core 1.1 Pluralization in Reverse Engineer - romiller.com
EF Core includes the ability to reverse engineer a model from an existing database – using the Scaffold-DbContext command in Visual Studio, ...
Read more >
Entity Framework Core with Existing Database
This reverse engineering command creates entity and context classes (by deriving DbContext ) based on the schema of the existing database. Let's create...
Read more >
Generating a model from an existing database
In this approach, you reverse-engineer a model from an existing database, resulting in the generation of an EDMX file that contains the model...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found