Entity Framework Core Inside Rider: UI Way
Do you work with Entity Framework Core? Would you like to manage entities right inside your IDE? Then you’re in luck! The Entity Framework Core UI plugin for Rider helps you effectively manage everything you need in EF Core. In just a few clicks you’ll have produced the necessary EF Core boilerplate code that’s necessary for database operations.
In this guest blog post, author Andrew Rublyov demonstrates the powerful features behind the Entity Framework Core UI plugin for Rider. It’s free, too! Let’s get started.
To work with Entity Framework Core, it is usually a good idea to use its powerful command-line tools –
dotnet ef. They allow you to perform a full range of tasks, from creating migrations and updating the database to generating SQL scripts from the migration code and creating executable bundles from them.
However, most commands are called quite often during development, and the lack of a convenient user interface can slow down your development process as a whole.
This article touches on the new plugin, Entity Framework Core UI for JetBrains Rider, which allows you to execute most
dotnet ef commands in a more user-friendly way.
We’ll walk you through these topics:
- Preparing the project
- Adding migrations
- Updating the database
- Existing database scaffolding
- Other features
The easiest way to install this plugin is directly from the IDE.
To do this, go to Settings | Plugins | Marketplace and search for “Entity Framework Core”:
After the plugin is installed, restart Rider:
Preparing the project
For the plugin to work, a supported version of the global
dotnet ef tool (3.1 or later) must be installed.
dotnet ef has not yet been installed, you will be prompted to install it when you open an Entity Framework Core–enabled project:
When you click Fix, the necessary tools will be installed in the background.
For supported projects to appear in dialog boxes, one of the following NuGet packages must be installed in the startup project:
Note that projects with a target framework below
netstandard2.1 are not currently supported.
All dialog boxes are available in the context menu of the project in the Solution Explorer tool window, under Tools | Entity Framework Core:
Note that the dialog boxes are adjusted to the current version of
dotnet ef, so not all options may be available for your environment.
Both the Migrations project and the Startup project can be used to access the dialog boxes. When executing any command through the dialog box, the current pair of projects will be saved to make it easier to execute commands on these projects in the future.
When you need to create a new migration, you can use the Tools | Entity Framework Core | Add Migration dialog:
Build options contain the default values. In most cases all you’ll need to do is select the correct Migrations project, Startup project, and DbContext class.
If this is the first migration in this project, the migration name will be “Initial”.
The DbContext field only displays the available migrations in the selected Migrations project.
Skipping the build process (
--no-build) is not recommended unless your project specifically needs to skip it. Although this option reduces the execution time of the command, it is also a source of potential problems.
After you click OK, the corresponding
dotnet ef migrations add command will be executed in the background:
If an error occurs during the execution of the command, it will be displayed in the notification and in the Event log. You can try to execute the command again by clicking the Try again button:
Updating the database
Use Tools | Entity Framework Core | Update Database to update the database to the required target migration:
The Target migration field is responsible for the target migration. Feel free to use autocompletion suggestions in this field to find the necessary migration faster. The list of migrations is sorted, which shows their order depending on the creation date.
Note: It is also possible to enter the value “0” in this field to reset the state of the database before the first migration.
If you’re updating and you want to use a different connection from the one specified in the Startup project configuration, you can select an arbitrary Connection. This can be either a connection string or a connection name from the project settings (in the format
Name=ConnectionStrings:SomeName). This is important when you need to apply migrations to a specific remote environment.
Existing database scaffolding
The plugin can be used to work with database-first solutions, not only code-first ones.
To generate a
DbContext and models for an existing database, go to Tools | Entity Framework Core | Scaffold DbContext:
A full range of options is available to help you customize the generation of the final
DbContext. They are divided into tabs:
- Main: defines the main options, such as the connection string, the provider, the folder where the models will be generated, as well as various switches for adjusting attributes, matching table names, etc.
All of these options are consistent with those used in
- DbContext: options related to generating the
DbContextitself. Here you can change the name of the context, as well as the folder in which this context will be generated:
- Tables and Schemas determine which tables and schemas, respectively, will be used in the generation. To change the default behavior (generation of all tables or schemas), uncheck the Scaffold all tables or Scaffold all schemas checkboxes, respectively:
If the specified tables or schemas aren’t found in the database, they will be ignored.
As with Add migration, Output folder and Generated DbContext folder point to folders relative to the currently selected Migrations project.
After the command is executed, the generated classes will be located in the specified folders:
If you find yourself needing to remove the default database, select Tools | Entity Framework Core | Drop Database:
You can also remove the last created migration by using Remove Last Migration:
The Migrations project must have at least one migration to be used in this command.
Subscribe to Blog updates
Thanks, we've got you!
A Second Set of Bug Fixes: ReSharper 2023.2.2 and Rider 2023.2.2 Are Here!
ReSharper and Rider have just received their second set of bug-fix updates for the 2023.2 release! Let’s take a look at the most important issues that have been resolved in this update. ReSharper For the full list of resolved issues, please refer to our issue tracker. …
ReSharper 2023.3 Early Access Program Begins!
Hello everyone, The Early Access Program for ReSharper 2023.3 has started! Before you download the first EAP build, let’s take a look at what is in store for you. Working with aliases With this first EAP build, we are introducing support for C# 12’s ability for alias directives to refer…
Rider Kicks Off the Early Access Program for the 2023.3 Release!
The Early Access Program for Rider 2023.3 has just begun with the release of the Rider 2023.3 EAP 1 build. There are several ways to get your hands on it: Download and install them from our website. Use the Toolbox App. Install this snap package from the SnapCraft store if you’re using a c…
Eager, Lazy and Explicit Loading with Entity Framework Core
Entity Framework Core (EF Core) supports a number of ways to load related data. There’s eager loading, lazy loading, and explicit loading. Each of these approaches have their own advantages and drawbacks. In this post, let’s have a quick look at each of these ways to load data for navigational prope…