0

below is my code I am trying to pull data from database using entityframework. EmployeeDataContext class -

namespace _09032020_1.Models
{
    public class EmployeeDataContext : DbContext
    {
        public DbSet<Employee> Employees { get; set; }
    }
}

Employee model -

namespace _09032020_1.Models
{
    [Table("TbleEmployee")]
    public class Employee
    {
        public int employeeId { get; set; }
        public string employeeName { get; set; }
        public string employeeCity { get; set; }
        public string employeeGender { get; set; }
        public int departmentId { get; set; }
    }
}

below are the table columns.

enter image description here

here is the controller code

namespace _09032020_1.Controllers
{
    public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            EmployeeDataContext employeeDataContext = new EmployeeDataContext();
            Employee employee = new Employee();
            List<Employee> employees1 = new List<Employee>();
            employees1 = employeeDataContext.Employees.ToList();
            return View(employees1);
        }
    }
}

I am not getting data inside employeeDataContext

enter image description here

Please let me know if more info regarding config file requires.

Community
  • 1
  • 1
  • Does this help https://stackoverflow.com/questions/29985150/visual-studio-during-debugging-the-function-evaluation-requires-all-threads-to – JamesS Mar 09 '20 at 15:12

2 Answers2

0
  1. Create the table w data. The first column is primary key and identity.
  2. In an MVC project (you can use another type, also I am showing database first, and you can use another type, right click on the Models folder and add ADO.NET Entity Data Model named EmployeeDataContext. Choose EF Designer from database. New connection, and choose your db. Save connection as EmployeeDataContext and choose your table.

Put this in your code:

 using (EmployeeDataContext context = new EmployeeDataContext())
 {
    var emps = context.Employees.ToList();
 }    
kblau
  • 2,094
  • 1
  • 8
  • 20
0

I got my answer, I have written wrong table name as model attribute [Table("TbleEmployee")] My table name is TblEmplyee. So it should be [Table("TblEmployee")] As I changed this I abled to proceed forward.