Using the Scaffolding Templates

This guide takes you through the basic setup steps to get your project up and running with these templates. There are obviously many different ways to configure your project that will also work with these templates. As long as you have a model that is shared by your WCF Data Service and your MVC application you can pretty much configure it how you want.

You can download a sample showing the completed applicaiton here

Prerequisites

The following prerequisits are required in order to follow these instructions:
  1. NuGet Package Manager - http://nuget.codeplex.com/

Project Setup

Setting Up Your MVC Project
  • Create an MVC project in visual studio
  • Install MVC Scaffolding into your project
    Install-Package MvcScaffolding 
  • Download the Code Templates and extract them into your project directory
  • Include the CodeTemplates folder in your project

Setting Up Your Model
You will need to create a class library to contain a model that is shared between your WCF Data Services Service project and your MVC application. These steps assume that you are using cod first Entity Framework with POCO objects but any model that is supported by WCF Data Services should work.
  • Add a class library project to your solution
  • Define your model using POCO objects
  • Add a reference to your model project from your MVC project

Setting Up Your Service
You need to create a WCF Service application to host your WCF data service
  • Add a WCF Service Application project to your solution
  • Add a reference to your model project
  • Add a reference to the Entity Framework 4.1
  • Add a data context class that inherits from DbContext and add read/write properties for each entity of your model
  • Add a WCF Data Service to your project
  • Where it says /* TODO: put your data source class name here */ replace this with your data context class
  • Add the following line to the InitalizeService method config.SetEntitySetAccessRule("*", EntitySetRights.All);
  • You should now be able to run the service project and navigate to your service endpoint -tip if you have any errors when neigating to your service then add [ServiceBehavior(IncludeExceptionDetailInFaults = true)] to your service class to show the exception details
  • Copy the URL to your serivce
  • In your MVC project create an application setting named xxx with the value of your URL endpoint

Scaffolding your MVC project
You are now ready to scaffold your controllers, views and model in your MVC project. For each of your POCO entities use the following commands in the nuGet Package Manager Console use the following commands:
  • To scaffold the data context use
    Scaffold T4Scaffolding.DataServiceContext {"[[Your POCO ClassName]]"} {"[[Your MVC Project Name]]"}Context
  • To scaffold the view and controller use
    Scaffold Controller {"[[Your POCO ClassName]]"}


Enabling Read/Write behaviour through IUpdateable
In order to support create/delete and update operations you will need to modify you WCF Service to support IUpdateable
  • Add the following method to your WCF Service
        protected override ObjectContext CreateDataSource()
        {
            var ctx = new SampleDataContext();
            var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
            objectContext.ContextOptions.ProxyCreationEnabled = false;
            return objectContext;
        }
  • Replace the generic type with ObjectContext instead of your data context type

Setting the Custom Scaffolder as the Default Scaffolder

You can avoid the additional scaffolding step and scaffold the whole lot in one command by setting the default scaffolder for a DbContext to the custom DataServiceContext scaffolder. To do this run the following command in package manager:

    Set-DefaultScaffolder DbContext T4Scaffolding.DataServiceContext

You can then scaffold everything using the command

    Scaffold Controller {"[[Your POCO ClassName]]"}

Last edited Jul 7, 2011 at 9:34 AM by owenssam, version 29

Comments

ActualRandy Jan 14, 2015 at 5:23 PM 
Hi Owenssam, thanks for creating this project and sharing it. It looks like a great idea, so I hope you will appreciate some information regarding it. As a fellow Codeplex contributor, I know it is sometimes challenging to share your code in a manner that works for everyone.

- I have Visual Studio 2012 at work, when I run 'Install-Package MvcScaffolding', I get the following message:
New-Object : Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
+ $newProvider = New-Object <<<< $powerShellScaffolderProviderType($mvcScaffoldingProvider)
+ CategoryInfo : NotSpecified: (:) [New-Object], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.NewObjectCommand

I tried adding a reference for 'System.Management.Automation' but it was not available, perhaps it only works for older versions of VS?

Also, you might take a look at your link above, i.e. "Download the Code Templates". When I click that link, I receive the message "The specified release was not found."