Data Annotation table columns

asp.net-mvc asp.net-mvc-4 c# entity-framework-6

Question

public partial class Employee
{     
    public int Emp_Id { get; set; }
    public string Emp_Name { get; set; }
    public string Emp_City { get; set; }
    public Nullable<int> Emp_Age { get; set; }
}

This is generated class for table Employee using EF. I want Emp_Name change to EmpName in application without changing table structure. But using below changes using data annotation dbEntity.Set<Employee>().ToList(); getting exception

"The entity type Employee is not part of the model for the current context."

System.InvalidOperationException.

How can solve this

public partial class Employee
{ 
    public int Emp_Id { get; set; }
    [Column("Emp_Name")]
    public string EmpName { get; set; }
    public string Emp_City { get; set; }
    public Nullable<int> Emp_Age { get; set; }
}
1
0
6/7/2017 7:19:36 AM

Accepted Answer

You don't need to change the column's name. You can easily use [Display] or [DisplayName] attribute like this:

Add using System.ComponentModel.DataAnnotations; to your using directives first then:

[Display(Name = "EmpName")]
public string Emp_Name { get; set; }

Or:

[DisplayName("EmpName")]
public string Emp_Name { get; set; }
0
6/7/2017 2:05:12 PM

Popular Answer

It seems you are using Database First approach. Do the following to change the property name without changing the column name:

  1. Just double click your .edmx file and it will show the tables.
  2. Find your table, right click the column name > Rename
  3. Give it a new name of your choice

It will keep this name and if you update your EDMX, it will still keep it. However, if you delete the EDMX and regenerate it, then you would need to do the renaming again.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow