Update: Colin Blair has kindly taken over the Scaffolding support, for the latest go get RiaServicesContrib.EntityFramework.Scaffolding. Since the WCF Ria Services team completed their support fo...

Simple WCF Ria Services EF 4.1 Scaffolding

Update: Colin Blair has kindly taken over the Scaffolding support, for the latest go get RiaServicesContrib.EntityFramework.Scaffolding.

Since the WCF Ria Services team completed their support for EF 4.1 (and Code-First), and released it as a NuGet package, it seemed obvious that we would also have scaffolding ‘enabled’ for quickly generating the bulk of the DomainService methods. Unfortunately, this did not yet exist, so as I wanted to get to know NuGet more deeply and did some twiddling with Scaffolding, I got started to create a new custom Scaffolder.

Basically, I started by creating a new custom scaffolder in my current project as per Steve Sanderson’s post. I then fiddled around reading the code, opening other packages to find more useful snippets using the NuGet Package Explorer.

I’m also using the DbContextExtensions as described in the a forum thread by Remco Blok/Arthur Vickers, they get included into your project. But I do hope the WCF Ria Services Team just add them ‘natively’ to their dll.

To use the Scaffolder, just type:

PM> Scaffold RiaEFDomainService Component KracomContext

Which is basically:

PM> Scaffold RiaEFDomainService <ModelType> <DbContext>

To get the package, make sure you have selected the Web Project first!

PM> Install-Package RiaServices.EntityFramework.Scaffolding

A note about the name: I realize that naming it the way I have now is perhaps presumptuous. In essence, I do not want to own this package, nor do I think I’m the one to keep it going.

  • I am hardly an expert in EF 4.1, RIA or Scaffolding
    Going forward, it would be nice to have best practices and further scaffolding support added to this package, to allow occasional WCF RIA users to benefit from the experience from the experts.
  • This is so easy, it should (have been) be part of the standard offering.
    So naming it in this way, I hope the MS guys that have done a great effort in providing the EF 4.1 support, can take this and add it to their portfolio.

 

Example:

Create a new Silverlight Business Application

image

Install the RiaServices.EntityFramework.Scaffolding package – make sure that the *.Web project is set as the Default project.

image

The package depends on the T4Scaffolding and the RiaServices.EntityFramework packages, so NuGet will download and install those for you.

image

Now create a new Class in the Models folder of the Web project.

public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int? CategoryID { get; set; }
public Decimal? UnitPrice { get; set; }
public bool Discontinued { get; set; }
}

Back in the Package Manager Console scaffold a DbContext (part of T4Scaffolding)

PM> Scaffold DbContext Product SampleContext

image

Now you are ready to scaffold the DbDomainService

PM> Scaffold RiaEFDomainService Product SampleContext

image

And it generates this simple (yet effective) code:

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using BusinessApplication1.Web.Models;

namespace BusinessApplication1.Web.Services
{
[EnableClientAccess]
public partial class SampleDomainService : DbDomainService<SampleContext>
{
#region Scaffolded CRUD for Product

public
IQueryable<Product> GetProducts()
{
return this.DbContext.Set<Product>();
/* Add optional Includes
.Include("relatedEntityName");
*/
}

public void InsertProduct(Product entity)
{
this.DbContext.Insert(entity);
}

public void UpdateProduct(Product current)
{
this.DbContext.Update(current);
}

public void DeleteProduct(Product entity)
{
this.DbContext.Delete(entity);
}

#endregion Scaffolded CRUD for Product
}
}
development
Posted by: Rudi Larno
Last revised: 06 Jun, 2012 10:23 AM