.NET Core Entity Framework quick Database first Scaffold

Hey, a quick tip for those dealing with .NET Core on Mac or Linux, even on Windows with Visual Studio code.

The following snipped will help you generate the entire database as models for you to use.

  1. In your project or in the machine, make sure you have dotnet ef installed
  2. Open a Terminal/Console
  3. Navigate to your project root path
  4. From there type the following

dotnet ef dbcontext scaffold “Server={YOUR AMAZING SERVER PATH};Database={THEGREATESDATABASENAME};User Id={THEPOWERFULUSER};Password={THENASAPASSWORD}” Microsoft.EntityFrameworkCore.SqlServer -o Models -f

The parts

  • dotnet ef tells the terminal to use dotnet entity framework
  • dbcontext scaffold here we are telling, “hey, I want to create models for my entire database”
  • The part between the quotes (“”) is a SQL Server String Connection
  • Microsoft.EntityFrameworkCore.SqlServer the database driver used for the connection
  • -o Models -o identifies the output of the clases that will be generated, in this case I put everything inside Models folder, but you can place that anywhere.
  • -f this force an override of any existing file, is not mandatory, but I always set that because if I change any file from the database it will be included, say for example, update the database
  • Additional you can set a -v to the end of the line to get all that’s going on with the transactions but is not required

And that’s it, everything you need to update your data models from the database, just run this.