Dynamic Data - Make Friendly Column Names?

最后都变了- 提交于 2019-12-06 09:27:47

问题


I've created a Dynamic Data project with an Entity Framework model. It works nicely. But, right now it shows all my database tables with the db column names - which aren't always the most friendly (e.g. address_line_1). How can I got about giving these more friendly column titles that will display to the end user?


回答1:


You should use Metadata classes to add additional annotations:

[MetadataType(typeof(MovieMetaData))]
public partial class Movie
{
}


public class MovieMetaData
{
    [Required]
    public object Title { get; set; }

    [Required]
    [StringLength(5)]
    public object Director { get; set; }


    [DisplayName("Date Released")]
    [Required]
    public object DateReleased { get; set; }
}

http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs - find Using Data Annotation Validators with the Entity Framework

Attributes are used not only for setting display name, but also for validation, turning visibility, order or how data should be presented. You should look into it if you want to use Dynamic Data Entities project.




回答2:


you can put a

[DisplayName("A fancy column name")] 

attribute above the column names in a partial class of the generated one.

Grz, Kris.




回答3:


When we are working with VB.NET is important to set the value as PROPERTY.

Use:

<DisplayName("Name")> _
Public Property FirstName As Object

instead of

<DisplayName("Name")> _
Public FirstName As Object

If you won't do that, you'll receive an error message




回答4:


In order not to loose changes every time you update the entity from database, you need to create another class file outside of the designer .cs files like this:

namespace ModelCustomers
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [DisplayName("Table Name")]
    public partial class My_Class
    {
    }
}

Now, even if you update the entity you still have the changes from your own file.



来源:https://stackoverflow.com/questions/3062866/dynamic-data-make-friendly-column-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!