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

ASP.NET.NET MVCC#

Avatar of undefined
Last Comment
Jhovyn Marcos

8/22/2022 - Mon
Pavel Celba

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jhovyn Marcos

ASKER
Just adding attribute [key] to my classes :)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck