Running Entity Framework (Core) commands in Rider

A while back, we received a very interesting question: how can we run Entity Framework commands like adding migrations or updating the database, in Rider?

In Visual Studio, Entity Framework commands like Add-Migration and Update-Database are typically run in the Package Manager Console. This works great, but unfortunately, it isn’t very portable. These commands are PowerShell-based, and the Package Manager Console ties to several Visual Studio-specific objects making it impossible to host it elsewhere.

No need to worry, though! With Entity Framework Core, Microsoft provides command line tools that work cross-platform, which in this case means in any IDE on any supported operating system. Let’s see how to get started!

Adding package references

The first thing to do would be adding package references. There are a few packages we’ll be needing, the first four are typically already installed when working with Entity Framework Core:

Edit for .NET Core 3 – the package names have been changed for .NET Core 3, and are now:

We can add package references using the NuGet tool window or by hand-editing the .csproj file. Since there are a couple of references to add, let’s select the project in the solution explorer, Jump to Source (F4) and manually add the required package references.

Edit .csproj file using Jump to Source

(Here’s a gist if you want to copy-paste the references)

Note that Microsoft.EntityFrameworkCore.Tools.DotNet is added as a “.NET CLI tool” reference, meaning it will extend the command line dotnet command.

Once added, Rider will run a package restore and download all required packages onto our system. We can now execute Entity Framework Core commands!

Entity Framework Core commands

Commands can be run from the built-in terminal (double-shift and type “terminal”). By default, the terminal opens in our solution folder. Since the Entity Framework Core command line tools are project specific, we’ll have to cd into the project folder. To test if we’re in the correct folder, run dotnet ef. If we see an Entity Framework unicorn in ASCII art, we’re ready to go.

Usign Rider terminal to run Entity Framework command line tools

If you see an error “No executable found matching command dotnet-ef”, you may have to run a manual dotnet restore from the command line first, or explicitly run dotnet tool install --global dotnet-ef to install the command line tools.

Let’s start by scaffolding a new database context, using the dotnet ef dbcontext scaffold command. It takes a connection string and the driver package to use, and we’ll also give it a name:

dotnet ef dbcontext scaffold "Server=(localdb)\MSSQLLocalDB;Integrated Security=true;" Microsoft.EntityFrameworkCore.SqlServer -c AcmeDataContext

This adds a file named AcmeDataContext.cs, which implements the data context we’ll be using while developing our application. Let’s add a Person class, as well as a DbSet<Person> inside of our AcmeDataContext:

By adding the DbSet<Person>, we’re telling Entity Framework Core to include and track a set of Person objects. In other words: we’re telling it we want to store some data in a table.

We can now create an initial database migration. In Entity Framework, migrations can be executed one by one to bring a database schema to the latest version (or rollback to an older version). Let’s call it “AddPerson”:

dotnet ef migrations add AddPerson

This generates a couple of things. It’s our first migration, so a folder named “Migrations” is added to our project. Under this folder, several files are created, some of them prefixed with a number. This number is a timestamp used for ordering the migrations that will be run, making sure our database schema doesn’t get corrupted by applying migrations. These are the files on my machine:

  • 20170804084416_AddPerson.cs – the code that, in this case, creates the People table in our database and adds a primary key. We can hand-edit this one, as long as we have not applied the migration to our database yet.
  • 20170804084416_AddPerson.Designer.cs – designer information. Don’t touch it as it helps Entity Framework with some details about the migration we created.
  • AcmeDataContextModelSnapshot.cs – A snapshot of the current database model, used by Entity Framework Core to determine the database structure.

One more step: applying the database migration and bringing the database up-to-date with our model.

dotnet ef database update AddPerson

There we go: we can now use our Entity Framework Core data context in code and query, add, update and delete Person objects in there.

The dotnet ef command has some more options. Check the documentation for more info.

Connecting to our database with the Rider database tools

Working with Entity Framework is one thing, while developing we may also want to inspect the database schema or run arbitrary SQL queries against the database itself. We can do this using Rider’s database tools, which are based on our IDE for databases: DataGrip.

The database tools are available from the View | Tool Windows | Database menu (double-shift and type “database”). We can create a new database connection, and select the type of database we want to work with. In the first part of this post, we used SQL Server LocalDB, so we can add a new datasource that targets SQL Server (jTds). Do make sure to check the steps described on the DataGrip blog!

Create SQL Server LocalDB connection using jTDS

Once the connection is configured, we can see our tables and work with the data stored in there. We can export data, import data, run arbitrary queries, … A great companion to Entity Framework!

Rider database tools in action

Want to give it a try? Download Rider now! We’re eager to hear your feedback!

About Maarten Balliauw

Maarten Balliauw is a Developer Advocate at JetBrains, working on .NET tools and Space. He focuses on .NET, Azure, web technologies and application performance. Maarten is a frequent speaker at various national and international events. In his free time, he brews his own beer. Follow him on Twitter or check out his personal blog.
This entry was posted in How-To's and tagged , , , , . Bookmark the permalink.

29 Responses to Running Entity Framework (Core) commands in Rider

  1. Paul Reid says:

    Does this mean there’s no chance we’ll ever be able to use Rider for EF6 projects? If the package manager console is impossible to host elsewhere, then we’ll always need a VS license :(

  2. Pingback: Dew Drop - August 10, 2017 (#2539) - Morning Dew

  3. Chris says:

    A line in the gist file makes the ‘dotnet ef’ command return the error ‘Version for package Microsoft.VisualStudio.Web.CodeGeneration.Tools could not be resolved.
    ‘ on OSX.
    This line causes it:
    DotNetCliToolReference Include=”Microsoft.VisualStudio.Web.CodeGeneration.Tools” Version=”1.0.1″
    If you remove it or change the version to 1.0.0 it works though.

  4. Dawid says:

    Does anyone have any idea how to resolve this issue ?

    “Package ‘Microsoft.VisualStudio.Web.CodeGeneration.Tools 1.0.2’ has a package type ‘DotnetCliTool’ that is not supported by project ”

    I tried most of the stuff posted on the net, but nothing works, adding it via nuget nor editing csproj. This acutally causes the dotnet ef error.

  5. Maarten Balliauw says:

    This should be resolved by adding:

    <DotNetCliToolReference Include=”Microsoft.VisualStudio.Web.CodeGeneration.Tools” Version=”1.0.2″ />

  6. Dmitrij says:

    But how to add-migration on EntityFramework 6.0?
    I tried using inline tool, but tool did not apear with connection to MsSqlLocal.Dmitrij

  7. Dmitrij says:

    But how to add-migration on EntityFramework 6.0?
    I tried using inline tool, but tool did not apear with connection to MsSqlLocal.

  8. Christian Vasquez says:

    Hey guys,

    Has anyone tried using this in macOS? Whenever I run the command:

    dotnet ef dbcontext scaffold “Server=(localdb)\MSSQLLocalDB;Integrated Security=true;” Microsoft.EntityFrameworkCore.SqlServer -c AcmeDataContext

    I get the following exception:

    “System.PlatformNotSupportedException: LocalDB is not supported on this platform.”

    I’ve read about SQL LocalDB not being supported for macOS, then what would be the right command to execute?

    In case it helps, I’m already using Docker to use SQL Server for Linux and I’m able to connect to it from Rider or DataGrip successfully.

  9. Andrew Morris says:

    Is it possible to create a migration against a .net standard csproj? I have a .net core proj that references a .net standard proj where the db content and entities live. When trying to create a migration using the following, I get the following error:

    dotnet ef migrations add DbInit -p AndrewMorris.Hub.csproj -s AndrewMorrisPortal.csproj


    MSBUILD : error MSB1009: Project file does not exist.
    Switch: AndrewMorris.Hub.csproj
    Unable to retrieve project metadata. Ensure it's an MSBuild-based .NET Core project. If you're using custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.

    • From the error, it looks like the project is not being found. This could be because dotnet ef is called from the wrong directory. If you have a solution and those 2 projects, the command will have to be run from the directory that holds your solution file.

  10. ifnk says:

    No project was found. Change the current working directory or use the –project option.

    how can i solve it ?

  11. DaveW says:

    Is the nuget package reference list above still correct for EF Core 3? I keep getting errors when trying to add migrations or update database from a multi project solution. Have tried running the correct commands from all of the directories (Solution directory and all 3 project directories) and still get error. I have no db code in my startup (which is common problem as per Microsoft update core 2 to core 3 docs – mine is a fresh core 3 solution).

  12. Virgil says:

    i couldnt use dotnet ef and i kept getting the same error after using dotnet restore.
    so i looked it up and since i was using dotnet core 3 i got here :
    seemingly the problem is solved running the following as it solved it for me:
    dotnet tool install –global dotnet-ef

  13. Shea Martin says:

    Sadly using EF Code First in Rider has not improved from the time of writing this article.

    Every time I perform an entity framework action in the I have to fill in all the information before running a command: project, configuration type, config project, config file, config source, log level, etc. It is so painful and error-prone that most people I know just keep Visual Studio open just so they can run the EF commands like add-migration and update-database on the PM console of VS. Is there anyway we can set the default values for a project? This feature was clearly developed by someone who does not do EF code first development… i.e., not dog-fooding the product.

  14. Oliver says:

    For anyone else that ends up and is banging their head against a wall

    dotnet tool install –global dotnet-ef

  15. Andrew says:

    Thanks Oliver,

    Just wanted to follow up, as your post was really helpful, that there is an additional dash in front of the global.

    dotnet tool install --global dotnet-ef

  16. Chester Husk says:

    I’d like to suggest that you all show how to set up the tools using a local tool manifest instead of the global install of dotnet-ef. Global installs are not portable, and the local manifest is the officially-supported way forward IMO. Related issue on the EF help itself is at

Leave a Reply

Your email address will not be published. Required fields are marked *