Create Your First Dynamic Data Entities Web Application

October 7, 2009

Create Your First Dynamic Data Entities Web Application

Two weeks ago I was Create Your First Dynamic Data Entities Web Application
asked if there is a way
to build a web back office
quickly. One thing that
popped into my mind was
the new ASP.NET Dynamic Data
framework that was shipped with
Visual Studio 2008 SP1. This post is the same introduction that I
made to the team members that asked me the question. Since
there weren’t any customizations needed in the back office they
needed the result was a standing back office in 5 minutes. That is
very productive.

Creating the Dynamic Data Application

The first thing to do is to create the Dynamic Data application.
This is a very easy job since we can pick one of Dynamic Data templates
which will generate the site. In Visual Studio choose File menu 
and New –> Project or New –> Web Site and then choose one of
Dynamic Data templates on the Web templates. The options are
Dynamic Data Web Application which will generate a general Dynamic
Data
application or Dynamic Data Entities Web Application which is
more appropriate to use with Entity Framework or LINQ to SQL.
I chose the second:
Dynamic Data Entities New Project

After pushing the OK button Visual Studio generates the Dynamic Data
application and we are clear to go. The result will look like:
Dynamic Data Entities Solution Explorer

Adding ADO.NET Entity Framework to the Application

When we have the application at hand now we need to create the Entity
Framework
model. This is very straight forward. You can choose to create a
different class library and put the model there (the preferred way) or you
can just put the model in the created project. For the simplicity of this demo
I chose the second option and the result is the model I’m using in all my demos:
Entity Designer Diagram   
Since this is only a demo for Dynamic Data and not for Entity Framework
I’m not showing how to create the model. If you seek information about how
to create an Entity Framework EDM I encourage you to read my
ADO.NET Entity Framework Tutorials series and in particularly the post
Entity Data Model (EDM) In Entity Framework.

Integrating the Context to the Dynamic Data Application

Now that we have the application and the Entity Framework model
it is time to combine them both. We do so by opening the Global.asax
file and in the RegisterRoutes we register the context using the model
object’s RegisterContext method. Pay attention that I set the
ScaffoldAllTables to true in order to show all the entities in my context.
If you want to show only part of them set the ScaffoldAllTables to false
and for every entity you want to show create a partial class and add there
the [Scaffold(true)] attribute.
After I register the context the RegisterRoutes will look like:

public static void RegisterRoutes(RouteCollection routes)
{
    MetaModel model = new MetaModel();
 
    model.RegisterContext(typeof(SchoolEntities), new ContextConfiguration() { ScaffoldAllTables = true });
    
    routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
    {
        Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
        Model = model
    });
}

That is all. Now we can run the Dynamic Data application and use it as

a web back office for our application.

My application looks like:

Running Dynamic Data Application

Summary

Lets sum up, I showed in this post how in 5 minutes of work you

can create a working web back office application. The Dynamic

Data
framework can be very useful in lots of scenarios such as creating

web back office for example.I suggest to give it a try and to find out

were it can fit your requirements.

DotNetKicks Image
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

one comment

  1. Randy YapOctober 24, 2009 ב 11:05

    It would be more useful esp to ASP.Net newbies like me who does not want to code more, to have a follow-up on this article by providing us with instructions on how to customize the web apps. Thanks. Good work btw.

    Reply