In my previous articles I have introduced T4 Templates for newcomers especially for those working in the domain of Entity Framework. In Part 3 I went through a few steps tweaking the default Template in order to force generating appropriately annotated POCO classes. I have demonstrated the results using an ASP.NET MVC2 Application accessing & validating Data via that annotated POCO class.
It turns out however, that sometimes more tweaks could be needed, especially while trying to add a WCF Data Service Layer, which would offer a RESTful Style OData Endpoint next to the Web-Application. This is a very nice and powerful extension to any Web-Application willing to offer Data-Services beyond the officially deployed application’s Web-Site. Theoretically it would be enough to right mouse button click on the MVC Application Project and add a New Item…, taking WCF Data Services from the list of available Items (Picture 1).
Let’s name the service WcfDataEmployeeSvc.svc whereas I’m referring to the Project downloadable at Part 3. We could configure thereafter the Service like Code 1 depicts. The only Problem with this solution is, that it will not Work while trying to access the offered Entity-Sets. Let say you would try to access the Service via this link: http://localhost:34287/WcfDataEmployeeSvc.svc/Users which immediately will result in an Exception stating:
The XML page cannot be displayed.
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
Error Message 1
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
var dataSource = new UserEmployeeDBEntities();
// Note prox creation is going to be disabled
dataSource.ContextOptions.ProxyCreationEnabled = false;
EmployeesViewData viewData = new EmployeesViewData();
int currentPage = page ?? 0;
viewData.PreviousPage = (currentPage <= 0) ? 0 : currentPage – 1;
viewData.NextPage = currentPage + 1;
viewData.Employees = Context.Users
.OrderBy(x => x.Id)
.Skip(currentPage * perPage)
if (viewData.Employees.Count() < perPage)
viewData.NextPage = currentPage;
Conclusion: In this article we have seen how to discard the virtual modifier at code generation time which will discard dynamic proxy behavior (real time change tracking) for the generated POCO classes.
Download Code here: