Create data Context Class in asp.net MVC 5 without using Entity Framework

Malam Malam 266 Reputation points
2024-06-17T05:04:37.0733333+00:00

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

Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-06-17T08:22:32.3666667+00:00

    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;
             con.Open();
             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"]);
                 employeeList.Add(employee);
             }
         }
         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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.