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.

6 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
    • hi,u need to go through from global.asax file and make some change which are mansion above Article.

      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

  • image1825-439x194_thumb.png
    Change the WebDeploy Port or why do I need port 8172?

      If you use WebDeploy on a server operation system you would usually use Port 8172. But what is this Port for and can I change it? Hint: For installing the WebDeploy I recommend this Blogpost. Port 8172 = IIS Management Service Default Port The Port 8172 is the default port of the IIS Management ...

  • Windows Phone Fonts & what if Visual Studio lies

    Today I was confronted with a little Problem: my Windows Phone App refused to show me the Font I choose – also other thinks didn’t work. Although the Visual Studio Designer did show the Fonts: Unfortunately there isn’t much left in the Emulator: Reason for this: Windows Phone doesn’t include all the typos Windows does ...

  • Json-Online-Tools: Viewer & Json2Csharp generator

      Wherever APIs are mentioned the JSON format I not far away. Since I’m using two tools regularly I would like to introduce you to them. JSON Viewer If you only see the JSON-Text you are usually not able to see the structure. With the help of JSON Viewer you can have an easy overview: ...

  • Windows Phone SDK & „System“-Icons

      Although the Metro Design focuses a lot on Typography Icons are still quite important. If you install Windows Phone SDK you will receive 36 Icons. You can find them here: C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Icons Unfortunately many Icons you might know from the common applications are not integrated. Pedro Lamas extracted 99 additional Icons ...

  • image1830-570x194.png
    How can I figure out if my ADFS 2.0 works?

      I was working with ADFS 2.0 (“Active Directory Federation Services”) for a while when this simple question crossed my mind: How can I figure out if the connection between ADFS and AD “works”? Here is a simple test… What is ADFS? If you need some “position of trusts” beneath the AD-boarders you choose an ...

Support us