Monday 21 March 2011

How to build a Dynamic Data website in 60 seconds

Do you want to be able to create a website to edit the records in your database without having to write any code?  Well with Dynamic Data and .Net it’s easy.

This step by step guide will automagically create your website so quickly that within a minute from now you’ll be able to create, read, update and delete the records in your database tables via your own web application.

For this demo I am going to use our old friend the Northwind database, a SQL Server sample database from Microsoft.

northwind

Step 1:  Open Visual Studio 2010 and create a New Project from the Web template “ASP.NET Dynamic Data Entities Web Application”

VS-add-project

Step 2: Right click on your project and select Add > New item...  Within the Data category choose “ADO.NET Entity Data Model”

add-entity-model

Step 3: Select Generate from database and click Next

Generate-from-database

Step 4: Click on the “New Connection…” button to add a new connection to your database, enter your server and database name, click Test Connection to make sure your user has access.  Assuming the connection is fine you can click OK.

add-connection 

choose-data-connection

Step 5: Click Next :)  That step was easy eh?!  Just notice the Entity Data Model name has defaulted to NorthwindEntities.  This is important because we’ll be referencing this object shortly,.

Step 6:  Choose the objects you want your website to interact with.  For this example I’m going to tick the tables box to select all tables.  I’ll manually remove the system table named sysdiagrams as we don’t need to be interacting with these tables.  Now click Finish.

choose-your-database-objects 

You can now see your tables within the designer view of your Entity model.

entity-model 

Step 7: For the last step you need to update the Global.cs file to reference your new Entity Model.  You do this by scrolling down to the “public static void RegisterRoutes(RouteCollection routes)” method, uncomment out the line highlighted below and enter the name of your EntityModel.

global

Change the line from this:


To this:


And that’s it - all done!  Your new website is now ready for you to use.  Just press F5, (you’ll be asked if you want to enable debugging click Ok) and then your new web application will appear.


debugging-not-enabled


The home page displays all your tables, ready for you to interact with.


 dynamic-data-site-home


Click on one of the tables, I’ve selected Categories, and the site will display a page containig the data, links for insert, edit and deleting the records.  You’ll also get a link to “View” referenced data like in Products in the example below.


dynamic-data-site-categories 


The “Insert” page for Categories:


dynamic-data-site-categories-add


The “Edit” page for Categories:


dynamic-data-site-categories-edit


The “View” page for for our referenced Products table takes use to the Products page.


dynamic-data-site-categories-products   




If you want to restrict which tables are displayed within the application go back to the line you edited within the Global.asax.cs file and set the ScaffoldAllTables = false;


Now create a new partial class for each table you want to edit and add the attribute [ScaffoldTable(True)] to the class, where the claasname matches the database table name.  For example, I’ve created one file called ScaffoldTables.cs and added two partial classes for the Category and Customer table in Northwind.


scaffold-tables


Now when you run your web application you’ll only be shown the tables you explicitly added partial classes for.


scaffold-tables-web


I hope you find this quick introduction to Dynamic Data useful - happy coding!


Source code:  DynamicDataDemo.zip

No comments:

Post a Comment