Skip to main content
A newer version of this page is available.

How to: Apply Attributes to Entity Properties when Using Model First

  • 2 minutes to read

In a Model First data model, object properties are declared in the designer-generated files, and you cannot decorate them with the required built-in attributes directly. The workaround is to apply the MetadataType attribute to a class, create the metadata class, and apply the required attributes to the properties of the metadata. The code below illustrates this approach using the data model from the How to: Use the Entity Framework Model First in XAF example.

Tip

A complete sample project is available in the DevExpress Code Examples database at How to: Use the Entity Framework Model First in XAF.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using DevExpress.ExpressApp.DC;
// ...
[MetadataType(typeof(EmployeeMetadata))]
public partial class Employee {
}
public class EmployeeMetadata {
    [Browsable(false)]
    public Int32 Id { get; set; }
}
//...
[MetadataType(typeof(TaskMetadata))]
public partial class Task {
}
public class TaskMetadata {
    [Browsable(false)]
    public Int32 Id { get; set; }
    [FieldSize(FieldSizeAttribute.Unlimited)]
    public String Description { get; set; }
}

A reference to System.ComponentModel.DataAnnotations is required to compile the code above. Here, we use the Browsable attribute to hide the Id properties (which are key properties in our data model) from the UI. The FieldSizeAttribute specifies that an unlimited number of characters can be stored in the Task.Description property, so a multiline editor will be used for this property in the UI.

See Also