使用Entity Framework的Database First自动生成扩展名为edmx的文件,然后又通过“包含视图的 MVC 控制器(使用Entity Framework)”的方法自动生成控制器和视图后。
In this video we will discuss, customizing the auto-generated index view.
At the moment, the "Index" view is using "Name" as the column header for both employee name and department name. This is because "Name" column is used in both the database tables(tblEmployee & tblDepartment) and entity framework has used these column names to generate "Name" property in Employee and Department classes that are auto-generated.
I want to change the depratment column header to "Department Name" instead of just"Name". To achieve this, add a class file with "name=Department.cs" to "Models"folder.
Copy and paste the following code in "Department.cs" file
[MetadataType(typeof(DepartmentMetaData))]
public partial class Department
{
}
public class DepartmentMetaData
{
[Display(Name="Department Name")]
public string Name { get; set; }
}
With these changes run the application and notice the column name is displayed asDepartment Name. This is achieved by using "Display" attribute that is present in"System.ComponentModel.DataAnnotations" namespace.
If you are wondering why can't we apply "Display" attribute directly to the auto-generated"Department" class instead of creating another partial "Department" andDepartmentMetaData class. We can do it. There is nothing stopping us from doing it, but every time the Department class is auto-generated, our custom changes will be lost. This is the reason for creating another partial class, and applying our changes.