1

Error message is :

Invalid column name 'EmployeeId' Invalid column name 'EmployeeId' Invalid column name 'City'.

{
   EmployeeContext employeeContext = new EmployeeContext();
   Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id); //This line is causing the error
}

But I got exact same database table with all matching columns, why it said I have invalid columns? where did I go wrong? I used codeFirst approach, and actually there are four columns in the table which are EmployeeID, Name, Gender, City, how come I don't have error in Name and Gender but just errors in EmployeeID and City? and error in EmployeeID appear twice?

Detail code

Employee class:

namespace MVCDemo.Models
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string City { get; set; }
    }
}

EmployeeContext :

namespace MVCDemo.Models
{
    [Table("tblEmployee")]
    public class EmployeeContext : DbContext
    {
        public DbSet<Employee> Employees { get; set; }
    }
}

Database table: enter image description here

3
  • To which database you think you are connected? Are you using DatabaseFirst or CodeFirst approach? Commented Jan 15, 2017 at 10:21
  • show your EmployeeContext and Employee model code. Commented Jan 15, 2017 at 11:12
  • @abdul added it to my post already Commented Jan 15, 2017 at 11:18

3 Answers 3

1

Why do you decorate EmployeeContext using [Table("tblEmployee")]? Should you do that with Employee class instead, right?

[Table("tblEmployee")]
public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

What I think is:

EmployeeContext employeeContext = new EmployeeContext();

That line is creating context against local database server. Please make sure you are giving correct connection string as a parameter:

EmployeeContext employeeContext = new EmployeeContext(connection-string);

Connection string should be taken from web.config or app.config depending on project you are working in.

2 Comments

I think I got the correct local databse server in web.config, if I change databasename I got another different error like invalid object something, but now I'm getting invalid columns
it is code first approach. Actually there are four columns in the table which are EmployeeID, Name, Gender, City, how come I don't have error in Name and Gender but just errors in EmployeeID and City?
0

The details you have provided is not good enough to understand your problem. First make sure you are properly connected to the database. Secondly, if City is navigation property in your entity class then maybe you are missing "Include" method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.