Using DBContext Connect Database in ASP.NET MVC 5

In this blog, you will learn about using DBContext Connect Database in ASP.NET MVC 5.

Continuing with the previous article, today, I’m sharing a simple example using DBContext Connect Database in ASP.NET MVC 5.
 
You can see my previous blogs below
Installing Entity Framework 6.x
 
Tools->Nuget Package Manager
 
 
 
Entity Framework
 
It helps us to map relationship object in the database used in ADO.NET
 
Okay, we need to create a database in App_Data directory, so you right click App_Data->New Item
 
 
 
After that, you open Web.config in the project and add the below code to connect database.
  1. <connectionStrings>  
  2.     <add name="demoASPEntities" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=demomvc5;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\demomvc5.mdf" providerName="System.Data.SqlClient" />  
  3. </connectionStrings>  
Create models in ASP.Net MVC 5
 
In the picture above, we create a User table, so I will build the entity model for the User.
 
Click Models-> add class User.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.ComponentModel.DataAnnotations;  
  6. using System.ComponentModel.DataAnnotations.Schema;  
  7. namespace MVC5_HelloWorld.Models  
  8. {  
  9.     public class User  
  10.     {  
  11.         [Key, Column(Order = 1)]  
  12.         [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]  
  13.         public int idUser { getset; }  
  14.         public string Username { getset; }  
  15.         public string Password { getset; }  
  16.         public int Lever { getset; }  
  17.     }  
  18. }  
Create Class Connect Database extends DBContext
 
DBcontext is used in Entity Framework, it’s a very important part to connect to a table in database and execute query (insert, update, delete)
 
Models/demoEntities.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using MVC5_HelloWorld.Models;  
  6. using System.Data.Entity;  
  7. using System.Data.Entity.ModelConfiguration.Conventions;  
  8. namespace MVC5_HelloWorld.Models  
  9. {  
  10.     public class demoEntities : DbContext  
  11.     {  
  12.         public demoEntities() : base("demoASPEntities")  
  13.         {  
  14.         }  
  15.         public DbSet<User> Users { getset; }  
  16.         protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  17.         {  
  18.             modelBuilder.Entity<User>().ToTable("Users");  
  19.             modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();  
  20.             base.OnModelCreating(modelBuilder);  
  21.         }  
  22.     }  
  23. }  
base(“demoASPEntities”)
 
The declaration name connect.
 
DbSet Users
 
We use the declaration table Users Controllesr/HomeController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MVC5_HelloWorld.Models;  
  7. namespace MVC5_HelloWorld.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         private demoEntities _db = new demoEntities();  
  12.         // GET: Home  
  13.         public ActionResult Index()  
  14.         {  
  15.             var data = (from s in _db.Users select s).ToList();  
  16.             ViewBag.users = data;  
  17.              ViewBag.title = "MVC5 - Hello World";  
  18.             return View();  
  19.         }  
  20.     }  
  21. }  
Views/Home/Index.cshtml edits the code below,
  1. @model MVC5_HelloWorld.Models.User  
  2.     <div class="container">  
  3.         <div class="row">  
  4.             <div class="col-md-12">  
  5.                 <h2>@ViewBag.title</h2>  
  6.                 <a href="https://hoanguyenit.com" class="btn btn-success">https://hoanguyenit.com</a>  
  7.                 <table class="table table-bordered">  
  8.                     @foreach (var user in ViewBag.users)  
  9.                     {  
  10.                     <tr>  
  11.                         <td>@user.Username</td>  
  12.                         <td>@user.Password</td>  
  13.                         <td>@{  
  14.                             if (user.Lever == 1)  
  15.                             {  
  16.                                 <span class="badge badge-danger">Admin</span>  
  17.                             }  
  18.                             else  
  19.                             {  
  20.                                 <span class="badge badge-warning">Member</span>  
  21.                             }  
  22.                         }</td>  
  23.                     </tr>  
  24.                     }  
  25.                 </table>  
  26.             </div>  
  27.         </div>  
  28.     </div>  
Demo