Databases Reference
In-Depth Information
[Display(Order = 20)]
public object Country { get; set; }
}
}
Both approaches become problematic as the size and complexity of your entity classes
increases. You should settle on a particular one early on and use it consistently through-
out the project to make maintenance easier.
At this point, the
Country
custom property you added to the Product entity is not
sortable—the
GridView
column generated by Dynamic Data for this property on the
Product list page appears as plain text and cannot be clicked. This is because the
UnleashedColumnProvider
object that the model provider creates for this property does
not have its
IsSortable
property set to
true
. Simply saying that a column is sortable is
not enough. Because sorting and paging are delegated by the
GridView
and
EntityDataSource
controls to the database server for a column to be sortable, it has to
have a valid
sort expression
.
For regular properties, like ProductName, the sort expression matches the name of the
property. However, the Entity Framework cannot infer mapping of custom properties to
database tables based on their names alone and need an explicit sort expression. Inside of
a custom page, you can specify it by setting the
SortExpression
attribute of the
UnleashedField
in page markup, as shown in the code snippet that follows. For pages
generated dynamically, the sort expression has to come from the metadata.
<unl:UnleashedField DataField=”Supplier” UIHint=”Parent”
DisplayField= ”Country”
SortExpression=”Supplier.Country”
/>
Specifying Sort Expression for Custom Properties in Entity Model
You can provide the sort expression metadata by applying a new data annotation attribute
to the custom property in the entity model. Here is how this would look:
partial class Product
{
[CustomProperty(Expression = “Supplier.Country”)]
public string Country { get { return this.Supplier.Country; } }
}
The source code of the
CustomPropertyAttribute
is shown in Listing 13.10. As you can
see, it is very short. At this point, its only purpose is to store the
Expression
value for the
entity property to which it is applied. The actual logic to take advantage of this informa-
tion has to be implemented in the Dynamic Data application.