Section 3: Adding Model to DB (Setting Up the DBContext)



Section 3: Adding Model to DB (Setting Up the DBContext)


Introduction:

In the last section, we have seen how to create a Model. In this section, we shall see how the model will be added as a table in the database using the Entity Framework. To Manage DB, we need SQL Server Management Studio (SSMS).


When we need to communicate with the Database, we need to connect to the database. To connect to the database, we need the information about the database, such as the server to connect to, credentials, etc. Let us see how to provide that.

What is a connection string?


Connection String is a string that specifies the information about the data source(in our example, its SQL server) and means of connecting to it. We need to add the "connection string" to our project. We usually add "connection string" in the appsettings.json in our project. 

Again, to be more precise, the connection strings will tell the application which server to connect to and what credentials to connect to the server. You can connect to any Database provider: SSMS, AWS Aurora, AWS Dynamo DB, etc.

The beauty of the ORM framework (such as Entity Framework) is that just by changing this configuration, you can connect to any Database. This will help to migrate our database with minimal code changes.



To add a connection String to our newly added project, Open appsettings.json and add the below connection string.

{
    "ConnectionStrings": {
        "DefaultConnection": "Server=(LocalDB)\\MSSQLLocalDB; Database=Hotel;Trusted_Connection=true;MultipleActiveResultSets=true"
    },
        "Logging": {
            "LogLevel": {
                "Default": "Information",
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information"
            }
        },
        "AllowedHosts": "*"
    }


This way, we have configured the connection string in our application. The next step will be to set up a connection to this database. 

To set up a connection to the database, we need to create an instance (object), which is a session with the database, and this instance can query and save the entities (models). This instance is called the DbContext.

To create a DbConext, create a folder Data under the solution, and add a class - HotelDBContext.cs.





HotelDBContext should be inherited by DBContext; DBConext is defined in the package, Microsoft.EntityFrameworkCore.

Next, we need a class using which we can query or add the data for a model. This class is called DbSet.
To create a DbSet for the Hotel model, we need to add public DbSet<Hotel> Hotels { get; set; }.

Our DbContext class looks as below:

namespace LearningMVC.Data
{
    public class HotelDBContext : DbContext
    {
        public HotelDBContext(DbContextOptions<HotelDBContext> options) : base (options)
        {

        }

        public DbSet<Hotel> Hotels { get; set; }
    }
}

Outroduction:

In this section, we have seen how we can connect to the database. In the next section, we shall see how we are using Entity Framework to create the table in the database.