Databases Reference
In-Depth Information
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Product)),
typeof(Product));
Because this code needs to run only once, you can place it in the static constructor of the
Product entity class itself, where it will be in close proximity to the MetadataType attribute
the extended class still needs. Here is a complete version of the Product class extended
using the same metadata interface used in the previous example:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(IProduct))]
public partial class Product : IProduct
{
static Product()
{
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Product)),
typeof(Product));
}
}
Validating Entities Before Saving
Although Dynamic Data takes care of validating the individual properties of entities when
they are created and modified through the dynamic web pages, this approach does not
protect you against errors when the entities are manipulated programmatically. Imagine a
console application that runs at a scheduled interval to import Products from flat files this
imaginary company, Northwind Traders, receives from suppliers. Because you have already
created the NorthwindEntities data model for the website itself, it would be beneficial to
reuse it in the import application, as it would allow you to reuse the business logic it
implements.
Suppose that even though the database schema allows you to store NULL values in the
UnitPrice column of the Products table, you decide that from the business point of view,
each product requires a UnitPrice before you can start offering it on the Northwind
Trader's website. Based on this decision, you could go ahead and modify the Product
entity metadata to apply the RequiredAttribute to the UnitPrice property as shown here:
[MetadataType(typeof(Product.Metadata))]
partial class Product
{
static Product()
{
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Product)),
typeof(Product));
 
Search WWH ::




Custom Search