Persisting data in .NET Core
Keeping business and data layers apart
This post is a follow up for Create an MVC Web App in .NET Core. We will be basing our example project on that post, in order to create an architecture using Entity Framework to persist data in our database. The code we’ll be using is for .NET Core 2.0, so if you’re using .NET Core 1, be sure to check out this post to see how to migrate to the next version.
The first step is adding what we’ll be calling a
Repository. Repositories are classes that will handle how we store our information in the database. Having them ensures the separation between the business and data access layers on our application.
Let’s create a new folder called
Repositories and add a new class, called
This new class will contain the basic operations you’ll be implementing, such as inserting new objects in your database, getting said objects, updating and deleting them. This repository is an abstract class (just like
BaseEntity)since every other repository will be inheriting from this one, allowing us to code these basic functions only one time. The “options” attribute constitutes the options required to instantiate a
DatabaseContext, which will allow us to connect to the database.
With that done, we now need to create a new repository, specific to the class we need to store. We’ll be creating a
UserRepository. To do so we need to specify the inheritance, and we’re done. Here, we can add any methods that have an impact on the database but are specific to the users.
UserRepository.cs will look like this:
As you can see, we don’t need to specify the methods in the
MasterRepository class. We only need to add any methods that apply to our model. In this case, we’re adding a
Now it’s time to add our new repository in our controller. First, we need a controller for our user model. The quickest way to create this controller and it’s basic CRUD views is with a scaffolding command. This will create a controller for our model as well as some useful views.
Make sure you have the following references in your
Controllers and views
We can, for example, scaffold the
UserControllerwith the following command:
dotnet aspnet-codegenerator controller -name UserController -m User -dc DatabaseContext — relativeFolderPath Controllers — useDefaultLayout — referenceScriptLibraries
The project should now look something like this:
If we check the project folder, we’ll see both the new controller and views. Here’s how the new controller will look:
With the controller created, we just need to add the
UserRepository within its attributes. The repository will be receiving the database context options (here named just
options) in order to create a
DatabaseContext, letting us do the necessary transactions in our database.
We’ll set it as a private readonly property and create a getter so that we can access it securely.
Here’s how the new
UserController will look like
We’ll also be deleting some things on the
User views. On both the
Edit views, make sure to remove the
UpdatedAt in the forms, since users won’t be changing those attributes manually.
That’s it! Now we can run our application and check out the new features.
Open your web browser and go to
http://localhost:5000/User. Here, you’ll see the new user administration site. Try adding a user! You should also be able to do any basic CRUD operations.
We can now add our own models and store the data in our database. Enjoy!
Make sure to stay tuned for the next post, where we’ll be showing you how to globalize your application!