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

  • image1528-570x194_thumb.png
    Introduction to Redis on Windows & Redis usage with .NET

      Redis belongs to the NoSQL data banks and you will find it in the group of Key-Value Stores. Redis is often named “Blazing Fast” and according to the Stackoverflow Thread it is used to be two time (while writing) and three times (while reading) quicker than MongoDB. Even if the comparison is a little ...

  • Automated Security Analyser for ASP.NET websites

    Evil Hackers are lurking everywhere and many Web-applications are delicately and share “too much” with the attacker. A quick (first!) overview offers the Tool “ASafaWeb”. All the website does is making a few requests and writing an Analyses including problem solving’s. There are no permanent disadvantages (bad requests/ DoS attacks and so on). Example: KnowYourStack.com ...

  • image1489-570x194.png
    „Sign in with Twitter“ for your own ASP.NET WebApp

      “Sign in with Twitter” is a popular practice to authenticate the users on your website. One advantage compared to an own registration is the lower inhibition for the user. But on the other hand Twitter doesn’t fess up with all the information’s and you will get into a kind of addiction. At the end ...

  • image1485-570x194_thumb.png
    CodePlex is going to be updated

      CodePlex the Microsoft Open Source Project Hosting Plattform hasn’t changed that much in the last few years and for a few times I thought Microsoft stopped the whole developing process. But now I found out that there is still life in the project. Maybe it is because of the success of GitHub or because ...

  • image1474_thumb.png
    What does Adobe in the flash-free web? Magazine-Style Layouts with CSS Regions!

      Adobe is well known for Photoshop and Flash but of course there is a lot more. According to the “Future Post” from Google Adobe declared one of their big subjects on a Blogpost. I’m talking about the W3C Working Draft to CSS Regions. Adobe cooperates with the WebKit Team and W3C on this. What ...

Support us