Practical ASP.NET

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:

  1. To integrate EF and Dynamic Data with an existing ASP.NET Web site.
  2. 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.

Some Limitations
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.

The list 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 the ProductId.

Within EF, there are limitations with what you can do in the Entity Model designer (listed here). 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 out).

Creating Associations
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.

About the Author

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/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube