Error: Invalid Column Name RRS feed

  • Question

  • User-304640242 posted

    I Currently have an asp.net MVC3 C# project that needs to get additional user data from an existing production server,  Which means I'm unable to modify the objects within that server in an attempt to resolve this issue.  When attempting to run this project I get the error "Invalid Column Name Team_team_id" as it tries to build an "Employee".


    Here are 3 tables I need to reference and the pertinent columns:

    Employees - employee_id / first_name / other_columns

    Teams - team_id / team_name / other_columns

    EmployeeTeamMaps - employee_team_map_id / employee_id / team_id


    My models reflect the tables with a public class of the singular version of the name, and properties for each of the column names.  I have placed within each model the table name where it should get its data from, as [Table("TableName", Schema="SchemaName')].

    Employee - employee_id / first_name / other_columns

    Team - team_id / team_name / other columns

    EmployeeTeamMap - employee_team_map_id / employee_id / Employee (referencing the specific employee model) / team_id / Team (referencing the specific team model)


    I have a controller that simply attempts to get Employee based on a supplied id, then build a model from that user.  Unfortunately it doesn't even get past the grabbing and employee part.

    public ActionResult TheController(int id)


    Employee newEmployee = _db.Employees.Single(r => r.employee_id == id); //get the error here :(

    //additional code...



    When looking at the additional exception details I can see that _db is trying to generate a select statement that includes "team_id" in the "Employee" model.  


    I expected the select statement to look something like:

    "Select [employee_id], [first_name] From Employees"


    Instead I get this:

    "Select [employee_id], [first_name], [team_id] From Employees" //notice the addition of the [team_id] column name, which is not present in the Employee model or the Employees Table.

    I am only somewhat aware of mvc3's preference to naming conventions and that the keys in the existing tables "employee_id", "team_id", "employee_team_map_id" are not whats expected.  Because its a production server I'm unable to change the existing columns, but I was able to create what I thought would be a work around, which was to create an "id" column within each table that was a calculated column of the actual primary key in the table (I added "id" to the "Employees", "Teams", Employee_Team_Maps" tables).  I then updated my models to reflect these changes, but unfortunately this yielded the same results, with the error changing to Invalid Column Name "Employee_id"


    I apologize as I'm fairly new to MVC3 so there are many things I'm still learning and to be honest I don't really quite understand what could be causing this.  I have searched for a possible solution spanning 2 days now and am hoping this will be my last stop.

    Thank you in advance.

    Thursday, March 22, 2012 8:29 AM


  • User1365230778 posted

    you don't post you three entity classes here, so, i guess the situation is that Employee has team_id property and Team property, you should add ForeignKey Attribute to your Team Property, something like:

    public class Employee
     public int employee_id{get;set}, public int team_id{get;set}, [ForeignKey("team_id")] public Team Team{get;set;} }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 23, 2012 4:42 AM