When you let the Entity Framework Designer in VS2008 generate the model from your database, the navigation properties of your objects will be based on the table names. Thus, assuming the presence of tables Customers and Orders and the presence of a foreign key from Orders to Customers, the designer will generate a business object class Orders with a collection property named Customers (cf. the ubiquitous Northwind database). This is less than ideal for two reasons:
- I don’t like the plural form of the property Customers on an order since it identifies a single customer, namely the customer having placed the order
- What happens if you decide to add yet another foreign key referencing the Customers table from Orders (maybe the customer was prompted to place the order by someone else – it’s a bit contrived but you get the idea)? What should we call the property on an order representing this relationship?
In the second case, the name of the referenced type/table is no good, since that is already in use. As it turns out, the EF Designer tries to name the property after the referenced type but adds a digit to avoid the name clash. Thus, the new property will be named “Customers1″. This conveys absolutely no information about the meaning of this property, so it is pretty useless.
The easy way to fix the problems mentioned above is to find the misbehaving properties in the designer, select them, press F2 (for ‘rename’) and rename them. This, however, is not a manageable solution if you are regenerating the EDMX regularly. In this case you will want to automate the process of opening up the EDMX in the XML Editor, finding the correct NavigationProperty in the CSDL region and performing a rename directly in the XML. In the example above the change might look like this:
<NavigationProperty Name=”Customers” Relationship=”NorthwindModel.FK_Orders_Customers” FromRole=”Orders” ToRole=”Customers” />
is changed to
<NavigationProperty Name=”Customer” Relationship=”NorthwindModel.FK_Orders_Customers” FromRole=”Orders” ToRole=”Customers” />
This may be automated using XSLT and nant.