Create data Context Class in MVC 5 without using Entity Framework

Malam Malam 226 Reputation points

How do I create a context class dbContext in MVC 5 without using the Entity Framework?

A set of technologies in the .NET Framework for building web applications and XML web services.
3,415 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor

    Hi @Malam Malam,

    DbContext Class needs to be implemented using Entity Framework. If you do not want to use Entity Framework, you can use ADO.NET, for example:

     public ActionResult Index()
         List<Emplyee> employeeList = new List<Emplyee>();
         string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
         using (SqlConnection con = new SqlConnection(CS))
             SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", con);
             cmd.CommandType = CommandType.Text;
             SqlDataReader rdr = cmd.ExecuteReader();
             while (rdr.Read())
                 var employee = new Emplyee();
                 employee.EmployeeId = Convert.ToInt32(rdr["EmployeeId"]);
                 employee.Name = rdr["Name"].ToString();
                 employee.Gender = rdr["Gender"].ToString();
                 employee.Age = Convert.ToInt32(rdr["Age"]);
                 employee.Position = rdr["Position"].ToString();
                 employee.Office = rdr["Office"].ToString();
                 employee.HireDate = Convert.ToDateTime(rdr["HireDate"]);
                 employee.Salary = Convert.ToInt32(rdr["Salary"]);
         return View(employeeList);

    Best regards,
    Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful