banner



How To Create A Dbcontext Class In Mvc

  • Azdev Xxi
  • Updated date Nov 01, 2019
  • 41.6k
  • 4

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

  • ASP.NET MVC 5 Hello World
  • ASP.NET MVC 5 Create Shared Layout Razor & ViewStart
  • ASP.NET MVC 5 Install Bootstrap

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 clickApp_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 { get ; set ; }
  14. public string  Username { get ; set ; }
  15. public string  Password { get ; set ; }
  16. public int  Lever { get ; set ; }
  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 { get ; set ; }
  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. public  ActionResult Index()
  13.         {
  14.             var data = (from sin  _db.Users select s).ToList();
  15.             ViewBag.users = data;
  16.              ViewBag.title ="MVC5 - Hello World" ;
  17. return  View();
  18.         }
  19.     }
  20. }

Views/Home/Index.cshtml edits the code below,

  1. @model MVC5_HelloWorld.Models.User
  2.     <divclass = "container" >
  3.         <divclass = "row" >
  4.             <divclass = "col-md-12" >
  5.                 <h2>@ViewBag.title</h2>
  6.                 <a href="https://hoanguyenit.com" class = "btn btn-success" >https://hoanguyenit.com</a>
  7.                 <tableclass = "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.                                 <spanclass = "badge badge-danger" >Admin</span>
  17.                             }
  18. else
  19.                             {
  20.                                 <spanclass = "badge badge-warning" >Member</span>
  21.                             }
  22.                         }</td>
  23.                     </tr>
  24.                     }
  25.                 </table>
  26.             </div>
  27.         </div>
  28.     </div>

Demo

How To Create A Dbcontext Class In Mvc

Source: https://www.c-sharpcorner.com/blogs/asp-net-mvc-5-using-dbcontext-connect-database

Posted by: kennedyliaboarpood.blogspot.com

0 Response to "How To Create A Dbcontext Class In Mvc"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel