3

We are using Entity Framework Core for a large project with database-first design. We use the "scaffold" command to generate entities from the database. In other words, we are doing something like this in the NuGet package manager:

Scaffold-DbContext "Server=foo;Database=bar;" Microsoft.EntityFrameworkCore.SqlServer -Context Fubar -Force

In general, this does a wonderful job of generating entity classes for us.

But there is one annoyance. Microsoft has something called the InverseProperty Attribute for denoting inverse relationships. So if you have an SQL table of type Node, with a ParentId pointing back to the same table, the generated code looks like this:

public class Node {
    public Guid ParentId { get; set; }
    public Node Parent { get; set; }
    public ICollection<Node> InverseParent { get; set; } // the name is annoying
} 

I understand why Microsoft needed a convention here. They don't know what the property names are going to be, and they need a way to invert them when generating the relevant navigation properties.

But in practice for us, the property that is getting inverted is Parent. And time after time, therefore, we see InverseParent, InverseParentFoo, and the like. It would be considerably better if the generated names were Children, ChildFoos, and so on.

We regenerate our entities pretty often. If the choice is between introducing a tool and living with the silly name, we would likely choose to live with the silly name.

Any suggestions for changing the generated name?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
  • 1
    We had similar problem and simply wrote powershell that we ran after creating classes with renames. When we looked we couldn't find way of customising the generated classes when they were created – GraemeMiller Jun 29 '18 at 16:50
  • 1
    As the word "scaffolding" suggests, the tool isn't aimed at ongoing model maintenance. When not working code-first, you're supposed to keep the database model and the class model in sync manually after scaffolding gave you a jump start. Database-first was abandoned in ef-core and so far no one has filled the gap yet (AFAIK). – Gert Arnold Jun 29 '18 at 21:11
  • 1
    @GraemeMiller whatever they decide to abandon, a lot of devs prefer design database before writing code. Personally I like how classic EF does all the namings, and "InverseParent" seems like "meh, why bother" – Alexander Selishchev May 17 '19 at 10:09

1 Answers1

1

Taking @GraemeMiller's advice, here's my powershell script. It also runs scaffolding, so you don't have to run multiple commands.

$connectionString = "your connection string"
$efProject= "your ef project name, also used a folder path for this script"

dotnet ef dbcontext scaffold $connectionString Microsoft.EntityFrameworkCore.SqlServer --context "MyContext" --force --project $efProject --data-annotations

foreach ($file in Get-ChildItem -Path $efProject *.cs) {
    (Get-Content $file.PSPath) |
    Foreach-Object { $_ -replace "InverseParent", "Children" } |
    Set-Content $file.PSPath
}
DharmaTurtle
  • 6,858
  • 6
  • 38
  • 52