Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaffolding Db Context and automatically remove the OnConfiguring method

For our ASP.NET Core project we scaffold the existing database using the Scaffold-DbContext in the Package Manger console.

Every time we do the scaffolding, a context class is generated together with all the entities and it contains the OnConfiguring(..) method which calls optionsBuilder.UseSqlServer(..) to configure the context to connect to the SQL server database.

We have our connection string defined in the appsettings.json file and don't want to use the OnConfiguring(..) method, so the question is, if there is a way to automatically remove the OnConfiguring(..) method, so that we don't have to do that manually everytime we run the scaffolding.

like image 671
AH. Avatar asked Oct 31 '17 06:10

AH.


2 Answers

The option you are searching for is: --no-onconfiguring

dotnet ef dbcontext scaffold
    "server=.\SqlExpress;database=MyDb;Trusted_Connection=True" 
     Microsoft.EntityFrameworkCore.SqlServer 
     --no-onconfiguring
like image 174
Chris Berlin Avatar answered Nov 13 '22 01:11

Chris Berlin


FYI there is a -NoOnConfiguring option coming in EF Core 5.0

-NoOnConfiguring Don't generate DbContext.OnConfiguring. Added in EF Core 5.0.

https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext

like image 9
Anthony Johnston Avatar answered Nov 13 '22 01:11

Anthony Johnston