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

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 http://www.devexpress.com/example=E4374.

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