Easy CRUD In ASP.NET Core With EasyData

CRUD in ASP.NET Core - the old way

If you try to search for information about implementing CRUD (create, read, update, delete) functionality in your ASP.NET Core application, you will most probably end up with one of the tutorials or video guides that describe how to create a controller and 4 different views for each entity (table) in your database. As a result, you will get a very basic implementation with only main functions (no paging, no sorting, no search, or filtering) and, let's be honest, quite lame UI.

Even if you use the scaffolding tool provided by Visual Studio it will take you about 10-20 minutes to set up the CRUD functionality for one model class. Multiply this time to the amount of DbSets in your DbContext, and you will get the amount of time necessary to get even basic CRUD implementation for all entities your application is going to work with. 

Moreover, managing all these controllers and views can be a challenging task, especially when your model classes are changed quite frequently (which is usually the case at the beginning stage of the project).

CRUD - the easy way with EasyData library

To avoid all the problems described above, you can use EasyData open-source library. Its main benefits include:

Super easy setup

Just a few lines of code, one HTML page, and you have CRUD for all data entities in your application.

Adaptivity

When you add a new DbSet to your DbContext, the CRUD functionality (both UI and API parts) will automatically be available for the new entity without any changes in the code or markup.

Declarative approach

All necessary information for API and UI (types, names, field sizes, validators) is taken directly from your DbContext. If you want to change the default behavior (e.g., to hide some field or set another validator for it), it’s possible to do with special attributes (annotations) for your model classes or via Fluent API (coming soon).

Beyond CRUD: search, paging, lookups

EasyData provides you with much more than just the basic CRUD functionality. It also automatically establishes paginations for big data sets, implements a simple but powerful text search, and allows users to enter values from connected tables via ad-hoc lookup dialogs.

The main magic here is that EasyData needs just your DbContext to implement all the features described above. It uses the information about model classes (their properties, connected entities, and special attributes) and automatically establishes the API for all CRUD operations, search, pagination, and lookups. The UI part is generated automatically with a vanilla JS library, so it can be used both in the projects with MVC or Razor Pages views and in any SPA applications (Angular, React, Vue, etc.).

Here's a quick introduction video on how to set up EasyData in your project and what it will look like as a result,

Quick overview of EasyData

Getting started with EasyData

To start using EasyData in your ASP.NET Core you will need to perform 3 simple steps described on the framework’s GitHub page: install the packages, add EasyData middleware in your Startup, and create a catch-all page (view) where all the UI will be rendered. The whole process will take no more than 10 minutes. 

We are not going to reproduce those steps here, so just use the link mentioned above for details.

EasyData - advanced tasks

The setup described in “Getting Started” uses the default settings. It means that all DbSets (tables) from your DbContext are opened for all kinds of operations and users can see and edit most of the fields in those tables. But what if we need to hide some tables or fields from users? Or if we need to grant read-only access to some parts of our database (so, users can see the values but can’t modify them)? All these problems are totally resolvable with EasyData’s annotations to your model classes and different extra options that can be applied to EasyData’s middleware and client-side code.

I’m going to just list some of them here to get you an idea of what is possible to do. For details and instruction please refer to the Advanced Tasks section of EasyData’s README document. 

  • It’s possible to change the default names of entities (tables) and any properties (fields) shown to users
  • You can hide any table or property
  • You can make any property a read-only or exclude it from a particular operation (e.g. it can be set during the insertion but cannot be modified after that).
  • You can define which properties will be shown in lookup dialogs
  • It’s possible to change the default endpoint of EasyData API (/api/easydata by default)
  • You can set the display formats for any field. For example, it’s possible to show a date field as “2021-Jun-22” or to use 2 digits after the decimal point for a field that stores floating-point values.

Wrap Up

EasyData is definitely the easiest and the quickest way for implementing CRUD operations in your ASP.NET Core application (provided that your project uses Entity Framework Core as well).

Currently, it works with .NET Core 3.1 and .NET 5. Obviously, all ASP.NET Core and Entity Framework Core versions that can work with the specified releases of .NET (Core) are supported. 

The authors can add support for ASP.NET 4.x as well if there are requests for that from the community.

EasyData is a perfect tool for quick prototyping of a POC (proof of concept) project when we already understand what data is needed and what to implement the basic operations with that data. However, I hope it will be possible to use this solution in a production environment at some point (after a few improvements).

So, don’t waste your precious time on a job that can be done in just a few minutes with the right tool. And don't forget to thank the authors of the library for their work by adding a star for the EasyData repository on GitHub.