Scaffolding DbContext and Models with EntityFramework Core 2.0 and the CLI

EF Core 2.0 has been out for a few weeks now. If you're looking at an ORM for your .NET application then EF Core should be at the top of the list of possible options. I say at the top and not the only one because depending on the project requirements, some features may be missing. For example, EF Core cut ties with .edmx so if you want to stick with the designer feature you will need to use EF6. There are other limitations so make sure you have a look at this post for API changes in 2.0 and also at the list of providers that may or may not be supported yet.

If EF Core fits your needs, then you're up for a treat, considering that it comes with significant performance improvements, smaller footprint and a lot of cool features to help you get your Data Access Layer (DAL) set up quickly. In most cases, you will use the Code-First approach, especially if it's a new project with no back-end. There are though those cases that you may already have a database in place. You don't want to hand-roll your models, definitely not! So how would you do it?

Using the EFCore Tools and CLI to scaffold your code

EF Core comes with an awesome CLI that can help you manage many aspects of EF Core in your project. You can have a look at all the available CLI options in the official docs - here.

The EF Core tools run in the context of the dotnet CLI.

Note: At the time of writing this post there's a known with support for .NETStandard 2.0. To work around it, you'll need to multitarget against .NET Core 2.0 or the full .NET Framework to allow the CLI commands to execute on your project correctly.

To scaffold an existing database you need to ensure that you've installed the appropriate NuGet packages. I'm using a Console App for this example so I don't have to worry about the issue I described above with NETStandard 2.0. On you project, you can either use the NuGet package manager or fire up the command line, navigate to your project and type the following commands:

dotnet add package Microsoft.EntityFrameworkCore -v 2.0.0  
dotnet add package Microsoft.EntityFrameworkCore.Design-v 2.0.0  
dotnet add package Microsoft.EntityFrameworkCore.SqlServer-v 2.0.0  
dotnet add package Microsoft.EntityFrameworkCore.Tools.DotNet-v 2.0.0  

We also need to edit the *.csproj file to enable the CLI tool integration. Open the .csproj file and add the following:

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>

Now we're ready to scaffold our code. In the command line, you can see the full list of options available when scaffolding from an existing database using the following command:

dotnet ef dbcontext scaffold -h

An example I used to scaffold an AdventureWorks database deployed on Azure SQL is provided below:

dotnet ef dbcontext scaffold "Server=tcp:<myAzureSQLServer>.databa  
se.windows.net,1433;Initial Catalog=<MyDB>;Persist Security Info=False;User ID=<myDBAdmin>;Password=<myDBPassword>;Multipl  
eActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.  
SqlServer -o Models -f -c DemoDbContext  

Dissecting the command:

  • ConnectionString (the Azure SQL connection string)
  • Provider (sql server in this instance)
  • -o for the output directory (Models)
  • -f to override previous auto-generated code
  • -c the name of the DbContext to use in the application

This resulted in the following code inside my project:

At this point I can instantiate the DemoDbContext in the application and start interacting with the database. As you can see, it took a couple of commands to install the NuGet packages, expose the EF CLI tools to the dotnet CLI context and scaffold the code from the existing codebase.

Pretty awesome if you ask me :)


  • Share this post on
comments powered by Disqus