Home .NET EF Core: Setting the name of the generated models in Scaffolding

EF Core: Setting the name of the generated models in Scaffolding

by admin

As it happens, in our old ASP.NET MVC project, the database table models were stored with identical names, i.e., lowercase, underscore, etc., and the DTO is already in the standard NET naming

This was done for a number of reasons.The main ones are: to explicitly specify the scope of the model; convenience in the construction of linq queries, when the names of tables and columns can be easily transferred from, for example, sql query.

Unfortunately, EDMX is no longer supported in .NET Core applications, but in the CLI command has a rather sparse set of features. So we had to do a little trick.

Perhaps in the 1.2 release. will add this option, but it’s not there yet, so we’ll have to go to extremes, temporary measures.

Step 1. Creating a project

First, let’s create a project that will contain all our models. This will be a .NET Core console application.

EF Core: Setting the name of the generated models in Scaffolding

Why Console Application and not Class library you ask. The reason is that Entity Framework only supports .NET Core CLI commands of these frameworks :

  • NET Framework 4.5.1 and newer. (“net451”, “net452”, “net46”, etc.)
  • NET Core App 1.0. (“netcoreapp1.0”)

And Class library uses netstandard, no fate. In any case, you can add the created console .NET Core application to the ASP.NET Core project.

Step 2: Setting up project.json

I will not beat around the bush, to describe what is responsible for this file I think there’s no point, we are not here for this.

{"version": "1.0.0-*", "buildOptions": {"debugType": "portable", "emitEntryPoint": true}, "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"}, "frameworks": {"netcoreapp1.0": {"imports": "dnxcore50"}}, "tools": {"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"}}

Step 3: Configuring Implemented Dependencies

When creating our Program.cs looks something like this :

namespace DomainModels{public class Program{public static void Main(string[] args){}}}

That’s what we’re going to expand on. So, let’s move on to a little research. After a little digging in the EF sources on GitHub I found implementation of class dependencies CandidateNamingService At scaffolding. After a quick glance at the code, I realized that it’s responsible for converting the database entity name to the .NET naming standard format. And all we have to do is override the GenerateCandidateIdentifierfunction so that it returns the original entity name.

Let’s expand our code to this variant :

namespace DomainModels{public class Program{public static void Main(string[] args){}}public class MyCandidateNamingService : CandidateNamingService{public override string GenerateCandidateIdentifier(string original){return original;}}}

Of course, you can put any logic you like in the GenerateCandidateIdentifier

Now we need to implement the dependency with AddSingleton. But where and how to do this? Since CandidateNamingService refers to design-time services we need to define a method ConfigureDesignTimeServices In the Program class:

namespace DomainModels{public class Program{public static void Main(string[] args){}public void ConfigureDesignTimeServices(IServiceCollection services){services.AddSingleton<CandidateNamingService, MyCandidateNamingService> ();}}public class MyCandidateNamingService : CandidateNamingService{public override string GenerateCandidateIdentifier(string original){return original;}}}

Done! Time to do model generation.

Step 4 Scaffolding

I will not go into details here, all the features of the CLI are described in detail here and we’ll take a look at the final version of the commands without the different settings :

dotnet restoredotnet rundotnet ef dbcontext scaffold 'string-connections-to-db' Microsoft.EntityFrameworkCore.SqlServer

That’s all you need to set up and generate your models. By the way, for better scaffolding I wrote a little PowerShell script I have an idea if someone wants to use it.

You may also like