Mark Tabor

Mark Tabor

  • 552
  • 2k
  • 467.4k

what is the context class in mvc

May 2 2016 10:53 AM
I want to make a dynamic menu in my MVC website i am very new to MVC i do not know more about it . I am following this article for dynamic menu generation 
http://www.thecrazyprogrammer.com/2015/02/create-dynamic-menu-in-aspnet-mvc.html
 
here is the code of, what is ProjectEntities() what is should write there instead of ProjectEntities() .
 

2
3
4
5
6
7
8
9
10
11
12
13
14
public static class MyMenu
{
/// <summary>
/// Get List of All Menu Items from Database
/// </summary>
/// <returns>Returns List<Menus> object</returns>
public static List<Menus> GetMenus()
{
using(var context = new ProjectEntities())
{
return context.Menu.ToList();
}
}
}
 

Answers (3)

0
Ravi Patel

Ravi Patel

  • 261
  • 6.8k
  • 1.4m
May 3 2016 1:12 AM
Hi ,
You can think of DbContext as the database connection and a set of tables, and DbSet as a representation of the tables themselves. The DbContext allows you to link your model properties (presumably using the Entity Framework) to your database with a connection string.
Later, when you wish to refer to a database in your controller to handle data, you reference the DbContext. For Example,
public class ProjectEntities: DbContext
{
public ProjectEntities()
:base("name=ProjectEntities")
{
}
public virtual DbSet<Menu> Menus{ get; set; }
}
is referenced later in the controller like
var context = new ProjectEntities()
for more details check this
https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
0
Munesh Sharma

Munesh Sharma

  • 102
  • 17.2k
  • 6.1m
May 2 2016 1:36 PM
context class is used to create datasource using entity framework.
this DbContext class store in
using System.Data.Entity namespace
0
Vincent Maverick Durano

Vincent Maverick Durano

  • 119
  • 14.9k
  • 4.2m
May 2 2016 10:58 AM
the variable "context" there refers to your entity data source. You can think of it as your database when using Entity Framework as your data access.
here's an article that might help you understand more about EF:
 
http://www.c-sharpcorner.com/UploadFile/8c19e8/building-web-application-using-entity-framework-database-fir/