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

Malam Malam 226 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?

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

Accepted answer
  1. Lan Huang-MSFT 28,276 Reputation points Microsoft Vendor
    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