GraemeMiller
GraemeMiller

Reputation: 12273

How Do I Add Display Name To An Inherited Property With EF Code First?

I have a class Movement inheriting from TimeBlock. TimeBlock is provided for me and I can't change it. TimeBlock provides a DurationDescription property and I want to display it. However I always use LabelFor etc which means I need to have Display metadata on DurationDescription so I can have "Duration Desc." etc.

How do I add metadata to an inherited class with EF Code First. Am I supposed to use buddy metadata?

Upvotes: 3

Views: 1662

Answers (1)

nemesv
nemesv

Reputation: 139798

Yes, you need to use the MetadataTypeattribute. It will work fine also with inheritance like with partial classes:

public class Base
{
    public string Prop1 { get; set; }
}

[MetadataType(typeof(ClassMetadata))]
public class Class : Base
{
    [DisplayName("My prop 2")]
    public string Prop2 { get; set; }

    class ClassMetadata
    {
        [DisplayName("My prop 1")]
        public string Prop1 { get; set; }
    }
}

On the UI the properties will be displayed as "My prop 1" and "My prop 2".

Upvotes: 4

Related Questions