Skip to content

Instantly share code, notes, and snippets.

@natemcmaster
Last active August 29, 2017 16:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natemcmaster/353f4ab3efb4514eeaec846df28f0e24 to your computer and use it in GitHub Desktop.
Save natemcmaster/353f4ab3efb4514eeaec846df28f0e24 to your computer and use it in GitHub Desktop.
CustomCandidateNamingService

How to customize naming convention of EF Core scaffolding in 1.0.0/preview2.

Warning: this uses internal APIs which are not officially supported. Future releases may break this.

On command line

dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Northwind;Integrated Security=true" Microsoft.EntityFrameworkCore.SqlServer

Output

Project snake (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Naming request: Order_Details
Naming request: Photo
...
using System;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class Startup
{
// Not officially supported. Using this is "I know what I'm doing" territory
public void ConfigureDesignTimeServices(IServiceCollection services)
{
// override the defautlt naming service
services.AddSingleton<CandidateNamingService, CustomCandidateNamingService>();
}
}
public class CustomCandidateNamingService : CandidateNamingService
{
// overrides https://github.com/aspnet/EntityFramework/blob/1.0.0/src/Microsoft.EntityFrameworkCore.Relational.Design/Internal/CandidateNamingService.cs#L25-L56
public override string GenerateCandidateIdentifier(string original)
{
Console.WriteLine("Naming request: " + original);
// original will be the table name, column name, index name, etc.
// In this override, you are free to do whatever you want.
// the base class will return PascalCase
var pascalCaseify = base.GenerateCandidateIdentifier(original);
return pascalCaseify;
}
}
}
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final"
},
"imports": "dnxcore50"
}
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
}
@recalde
Copy link

recalde commented Jul 2, 2016

Thank you! I changed this to just "return original;" so there would be no renaming at all, worked like a charm!

@codearoo
Copy link

Awesome. Made life easier for me too.

@fglebel
Copy link

fglebel commented Feb 9, 2017

Thanks!! Saved me also...uuff..

@Alexct72
Copy link

I tried with EF Core 2.0 but I did not work.
I used the following instruction:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=NameDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

@arichika
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment