HowTo: SQL Database web access with ASP.NET Dynamic Data and Entity Framework (hosted on IIS 6.0)

image I started my programming career with PHP and MySQL and I used very often a webbased mysql admin panel called "PhpMyAdmin".
It has (of course) not all features of the SQL Management Studios, but if I only want to have a quick look at the database it is very cool – and you only need a simple browser.
You can create such an tool with ASP.NET Dynamic Data and Linq2Sql or the Entity Framework within minutes.

Intro ASP.NET Dynamic Data
This feature was a part of the .NET 3.5 SP1. If you create a new projects you´ll see two different project templates:

image

Both templates are nearly the same, but there is a difference:

  • Dynamic Data Entities Web Applications: Use it only with ADO.NET Entity Framework
  • Dynamic Data Web Application: Use it only with Linq2Sql

If you try to use the EF in the wrong template you´ll get a runtime error: "The method ‘Skip’ is only supported for sorted input in LINQ to Entities"  (Source and more info)

Both templates take a datacontext (Linq2Sql or EF) and create at runtime CRUD and list pages. It use a templating system and you could change everything if you want it.

Step 1: Create ASP.NET Dynamic Data Entites Web Projekt
We choose the entity web project and should see the following project structure:

image

The "DynamicData" folder contains all the templates, pictures etc.:

image

"PageTemplates" holds all CRUD and the List templates. The "FieldTemplates" render the different types (e.g. a DateTime).

I´ll keep the default behaviour, templates and styles for this HowTo.

Step 2: Create the Entity Model
Now we need a entity model – that´s why we now need a database. I choose the Northwind sample database.
Now we create the model:

image

In the next screen you have to establish the connection to your database. Use ".\SQLExpress" if you have a local running SQL Express Server and add everything you want:

image

Step 3: Setup Dynamic data with the datacontext
This is the important step: Just add the datacontext to the DynamicData "MetaModel", read the comments and paste your datacontext into the metamodel:

MetaModel model = new MetaModel();

            //                    IMPORTANT: DATA MODEL REGISTRATION
            // Uncomment this line to register LINQ to SQL classes or an ADO.NET Entity Data
            // model for ASP.NET Dynamic Data. Set ScaffoldAllTables = true only if you are sure
            // that you want all tables in the data model to support a scaffold (i.e. templates)
            // view. To control scaffolding for individual tables, create a partial class for
            // the table and apply the [Scaffold(true)] attribute to the partial class.
            // Note: Make sure that you change "YourDataContextType" to the name of the data context
            // class in your application.

            //--> Code-Inside Change HERE!
            model.RegisterContext(typeof(NorthwindEntities), new ContextConfiguration() { ScaffoldAllTables = true });

NorthwindEntities is my datacontext and I set the "ScaffoldAllTables" to true. But be carefull with this setup – it allows anybody to create, read, update and delate everything on your model/database. Limit the access via configuration or (the easy way) the IIS settings.

Result

The result (after few minutes) :

image 
Overview

image 
List

image 
Details

image 
Edit


If you just need a plain admin panel for your application (and use Linq2Sql or EF) this is a very nice.

Dynamic Data on IIS 6

Dynamic Data use the Routing API like ASP.NET MVC, thats why you have such URLs:

http://localhost:52016/Alphabetical_list_of_products/Edit.aspx?CategoryName=Beverages&Discontinued=False&ProductID=1&ProductName=Chai

If you have only an IIS 6.0, you should read this great blogpost.

[ Download Democode (without DB) ]

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Currently there is no additional info about this author.

5 Responses

  1. Better to use linq to sql, instead of entity framework, for this purpose, since it provides one-to-one mapping from table to class.

    Reply
  2. Hi All!

    Has someone tried to modify the mappingSource of the DataContext at run-time before anything is excecuted. I want to able to change the datasource at run-time to point at different databases depending on who the User is.

    Right now it is using this code:

    base(global::System.Configuration.ConfigurationManager.ConnectionStrings["FAMP_ConnectionString"].ConnectionString, mappingSource)

    Thanks!

    Reply
  3. Hi, thanks for the example.

    I have a problem, I was able to view data but its showing error while updating, inserting , deleting

    Can please advise what might be the problem

    Thanks,
    Sam

    Reply
  4. I’m able to view the data from the table but I’m getting folling Error: while Inserting, updating, deleting the records

    Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Unable to update the EntitySet ‘Jobs’ because it has a DefiningQuery and no element exists in the element to support the current operation.

    Reply
  5. Informative. But I suggest to provide code beside menu ….

    Reply

Comment on this post

Recent Posts

  • image1452_thumb.png
    Javascript to Dart Translator

      Dart, a Google Javascript alternative was presented a few months ago and the web developer scene are a little bit unsure about the usability of Dart. To declare the language Google has translated the Javascript basics into Dart. The result is this “Translator”. In my opinion the name doesn’t find that well because it’s ...

  • image1366-570x194.png
    Twitter Bootstrap as UI-kit

      HTML and CSS are not foreign words for me but I regret, I’m not a Web designer – I see myself as a webdeveloper. But at least a dressy side is a must. But thank good there are some ready “Systems”. Twitter Bootstrap Twitter Bootstrap is a Toolkit for every kind of Web applications. ...

  • image1441.png
    Fix: the value ‚x‘ is not valid for Foo in ASP.NET MVC

      To get files into the MVC Controller Modelbinding from MVC is a clever method. But in fact it is a little bit complicated to set the error message if the connection failed. Example: public class RegisterModel { ... [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email address")] public string Email { get; set; } [Required] [Display(Name = ...

  • You Tube API – recall Video Meta files with .NET

      A loooong time ago I’ve blogged about how to access to You Tube with the Google Data APIs. After all that time there are several new ways how to recall files. Google offers You Tube a “simple” surface. If you prefer to do low-Level HTTP calls it is also possible. Aim: I want the ...

  • image1426-570x194.png
    MacBook Pro for .NET Developer – useful ore just pretty?

      I own a MacBook Pro (from 2010) for about a year now and because I’ve used to think about this Question since I have it, I’m going to blog about my experience now. My Notebook Configurations MacBook Pro April 2010 - 2,66 Intel Core i7 - 8GB RAM - 15’’ Glossy Display - Intel ...

Support us