Section 4: Pushing Model to the database

 


Section 4: Pushing Model to the database

Introduction:

In section 3, we have configured the code in our project to connect to the database. Next, we need to push the Model to the DB as a table. This process is called the Migrations in Entity Framework. Entity Framework deserves a separate tutorial, and I am planning to create it soon. However, this tutorial will cover most parts of Entity Framework.

This tutorial will use Entity Framework's Code-First to connect to a SQL Server database that will hold Hotel and Restaurant tables. The Code-First is the right choice for developers who want to code more; we will write the classes, and Entity Framework will create the database to match. By doing this,, we are spending less time setting up the data tier and more time coding. 
Code-First uses a process called Migrations to manage changes to the database; each set of changes is managed in a separate file. This is just like a git change. If you had used git as your source code repository, you would know that to can commit the changes, and you can anytime roll back the changes in git. Similarly, the changes you make in the database can be roll backed anytime using Migration commands. For instance, I will add a new column to the Hotel table in the DB, say, Menu. After a few releases, the customer/client doesn't need the Menu to be used. You can just roll back the changes,, and you are good to go!
For an introduction on Entity Framework, check this: Introduction to Entity Framework (chaiandwine.info)


Steps to add Entity Framework to our project:

a. Install Microsoft.EntityFrameworkCore Nuget Package. To install it, Go to Tools->NuGet Package Manager->Manage Nuget Packages. Next, search for Microsoft.EntityFrameworkCore and install it.



b. Next step is to set up the Models and Entity Framework in our project. In section 3, we have already set up the Model. However, we haven't st up the Enity Framework in our application. For setting up the Entity Framework, follow the below steps:


Entity Framework can be added to the application's configuration using AddDbContext in the ConfigureServices method of Startup.cs.

Add below lines in the ConfigureServices method to configure EF Core to the application configuration.

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<HotelDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddControllers();
        }

We have already created a Model for pushing this model to the database. Let us now start moving this model to the database. This process is called Migrations.

To move the model to the DB, we need to run a few commands. Firstly, we need to add migrations to our project. The command to add migrations to our project is

add-migration <migrationName>

This command will enable migrations to our project.

The next command is,

update-database

This will update the server database; if the database is not available, it will create one. If it's available, then it will update it.

We need to run these commands in the Package manager console.

To run the package manager console:



When we run add-migration addHoteltoDB, we may see the below error.



This error is because we don't have Microsoft.EntityFrameworkCore.Tools package installed. Hence, install this package from the NuGet package manager and again run add-migration addHoteltoDB.


Now, we will see a Migrations folder created:



Finally, let us run update-database to see the database in the SQL server.



When we open SSMS and check the database, we can see that the Hotel Table is created.



Outroduction:

I this section, we have seen how we can create a table in the database using the Entity Framework. We have also seen the way to configure the EF in our project.

In the next section,, section-5-repository-pattern, we shall see how to implement Repository Pattern and Unit of Work.

Happy Coding!