Link to home
Start Free TrialLog in
Avatar of Jhovyn Marcos
Jhovyn Marcos

asked on

ASP.NET - Why I am getting invalid column name using MVC4?

Hello...Please help me with this error I encountered.. "Invalid column name 'DepartmentId'"..Below is my Code

EmployeeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;

namespace MVCDemo.Controllers
{
    
    public class EmployeeController : Controller
    {

        public ActionResult Index(int departmentId)
        {

            EmployeeContext employeeContext = new EmployeeContext();
            List<Employee> employees = employeeContext.Employees.Where(emp => emp.DepartmentId == departmentId).ToList();
            return View(employees);
        }

        public ActionResult Details(int id)
        {

            EmployeeContext employeeContext = new EmployeeContext();
            Employee employee = employeeContext.Employees.Single(emp => emp.employeeId == id);

            return View(employee);
        }

    }
}

Open in new window

EmployeeContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace MVCDemo.Models
{
    public class EmployeeContext : DbContext
    {   
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Department> Departments { get; set; }


    }
}

Open in new window


DepartmentController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;

namespace MVCDemo.Controllers
{
    public class DepartmentController : Controller
    {   
        public ActionResult Index()
        {
            EmployeeContext employeeContext = new EmployeeContext();
            List<Department> departments = employeeContext.Departments.ToList();

            return View(departments);
        }

    }
}

Open in new window


Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace MVCDemo.Models
{
    [Table("employeeDetails")]
    public class Employee
    {
        [Key]
        public int employeeId { get; set; }
        public string employeeName { get; set; }
        public string employeeGender { get; set; }
        public string employeeCity { get; set; }
        public int DepartmentId { get; set; }
        
    }
}

Open in new window


Department.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace MVCDemo.Models
{
    [Table("employeeDepartment")]
    public class Department
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<Employee> Employees { get; set; }
    }
}

Open in new window


Im getting error here
List<Employee> employees = employeeContext.Employees.Where(emp => emp.DepartmentId == departmentId).ToList();

Open in new window

Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Could you please also post the employeeDetails table structure from your SQL server?  It seems the DepartmentId column is not present.
ASKER CERTIFIED SOLUTION
Avatar of Jhovyn Marcos
Jhovyn Marcos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jhovyn Marcos
Jhovyn Marcos

ASKER

Just adding attribute [key] to my classes :)