Integrating Entity Framework with an ASP.NET Web Site
Peter begins to explore the usability of Dynamic Data and Entity Framework by adding the simplest possible data model to his application.
In this column and in following ones, I'm going to look at integrating Dynamic
Data (part of ASP.NET 3.5 SP1) into an existing ASP.NET project. You'll be playing
along with me: I don't know if this technology is actually going to turn out
to be practical.
And, to be honest, this column -- and potentially the next few -- won't be
ASP.NET-specific because I'm going to be poking around in the Entity Framework
(EF). If you intend to use Dynamic Data, you need EF because it handles creating
the middle-tier objects you need to site between your UI and your relational
tables. You can't use LINQ for this because unlike EF, LINQ doesn't support
The first step in integrating EF with your site is to go to Add New Item on
the Website menu and select ADO.NET Entity Data Model. After naming your model
and clicking OK, you'll get a dialog giving you a choice between generating
your entity model from an existing database or starting with an empty model.
For full control, pick the empty model. (For generating a model from an existing
database option, see Roger Jennings' article "Generate Web Sites Automatically"
The entity model is added to Solution Explorer as an .EDMX file in Solution
Explorer and a new tab (named Model Browser) that gives you a tree view of the
To create your entity model, you drag an Entity control from the Toolbox onto
the design surface of your .EDMX file, give the Entity some properties, map
the Entity to a table (or a view or stored procedure), and finish by mapping
those properties to columns in the table. For instance, to create an entity
that represents the Orders table in the Northwind database, you'd begin by dragging
an Entity control onto the design surface.
You'd then add the properties to the Entity to represent data drawn from the
database. You get one property free (named Id with a datatype of Int32). To
add new properties, just right-click on the Entity's Scalar Properties bar and
select Add | Scalar Property. You can name each property and set its data type
in the Properties window. For my Orders Entity, I added properties called Address
There are some rules: First, do add properties for every primary key field
in the table. Second, don't add properties that would represent foreign keys
in your table (unless they're part of the table's primary key). Finally, mark
those properties that will uniquely identify the entity by right-clicking on
the properties and selecting Entity Key from the pop-up menu.
Mapping to Tables
With the entity's properties defined, it's time to map the entity to a table
and entity properties to table columns. You must first add to the Entity Model
the database tables that you intend to use. The easiest way to do that is to
right-click on the design surface and select Update Model from Database.
After selecting a database connection, you'll get a list of all the tables,
views and stored procedures in the database. Check off the tables you need.
However, those tables are just a means to an end: What you really want to do
is map your entity to one or more tables. If you want, you can even delete the
tables after adding them (the Entity Model seems to remember all the tables
it's ever known).
To start mapping your tables to your entities, click on an Entity and select
Table Mapping. This opens a window below the editor window named Mapping Details.
You can select the table you want to add to your entity from the drop-down lists
in this window. For my Order entity, I chose the Orders table.
The final step in creating the Entity is to map properties in the entity to
columns in the tables mapped to the entity. This is also done from the Mapping
Details window: Select a column from the table and, from the field's drop-down
list, select the Entity property that it maps to. If you've given your properties
the same name as the columns they map to, you'll find that those mappings are
already done for you. Once you've assigned columns to all of your properties,
you have an Entity.
This is obviously a simplistic design. In my next column, I'll sketch the page
I'd like to build, extend this model and look at some features and limitations
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/.