Extending an Entity Framework Model -- and Some Limitations
To support a real page, your entity model is going to need multiple entities and associations between them. Here's how to add them.
This column continues my exploration of both Dynamic Data and Entity Framework
(EF). As with my previous column ("Integrating
Entity Framework with an ASP.NET Web Site"
), I have two goals:
- To integrate EF and Dynamic Data with an existing ASP.NET Web site.
- To see if these two technologies can support typical business applications.
My goal is a master detail page using the Northwind database. On this page,
the user selects a customer and is shown a grid of orders for the customer.
When the user selects an order in the grid, the user is shown the OrderDetails
for the order. Ideally, the user will be able to perform all the CRUD operations
on both orders and order details.
To support the CRUD activities, I have to start with EF because it supports
updating (which LINQ doesn't). So, as with my previous column, this column continues
to explore EF.
In my previous column, I walked through the process of adding a single entity
to a data model. Obviously, to achieve my end result, I'm going to need a model
that includes OrderDetails, Customers and Products entities. In addition, when
displaying an OrderDetail, the user should be able to the Product Name, not
just the ProductId.
of supported mappings between tables and entities is long, including mapping
multiple entities to a single table and inheritance. On the other hand, EF won't
let me create an entity that maps to two tables (except for the most trivial
case where there's a one-to-one relationship between tables that share a similar
primary key). I can't, for instance, create an OrderDetail entity that includes
information about the Customer for that Order. I would assume that the user
wants to see the ProductName when looking at an OrderDetail, and not just see
Within EF, there are limitations with what you can do in the Entity Model designer
For instance, in the OrderDetails entity, I want to have a read-only, calculated
property called "ExtendedPrice" that returns the result of multiplying
UnitPrice by Quantity. I can't do that in the Entity Model designer but that's
a limitation of the designer, not of EF (at least, I think it is -- we'll find
Returning to the model that I created in my previous column, I added an OrderDetails
entity using the same techniques that I used with the Order entity. The real
change from the process that I used in that column was on the OrderDetail entity:
Since the underlying table has a compound key, I marked both OrderId and ProductId
as Entity keys.
The next step is to create an association between the Order and OrderDetails
tables. Most of the associations that you'll create are "one-to-many"
relationships. There are several different ways to add an association: I used
the Association tool on the Toolbox. But be aware -- don't try to drag and drop
the Association tool! Instead, first click on the Association tool, click on
the entity on the "one side" of the relationship and then click on
the entity on the "many side." A line will appear in the design surface
joining the two entities and new entries will appear in each entity under the
entity's Navigation Properties bar. If you click on the entities in the wrong
order, delete the association and try again.
While the association appears on the design surface, it's not fully defined.
You need to right-click on the association line and select Table Mapping from
the pop-up menu. This will display the Mapping Details window below the editor
window. From the drop-down list in Mapping Details, select the table on the
"many side" (in my case, the OrderDetails table). You then need to
assign the entity properties used in the relationship to the columns involved
in the relationship, again by selecting from drop-down lists.
Now that I've got the basic model that I need, I just right-click on the design
surface and select Validate to make sure that I've got everything right. After
clearing up any errors (mostly around matching property datatypes to column
datatypes), I rebuild the Web site to compile the build and enable me to use
the entity model elsewhere on my site.
Now it's time to build the page...in my next column.
Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.