Saturday, July 10, 2010
PLINQO or LINQ to SQL “Supercharged”: Getting Started
By the end of this post you should know how to get a working Visual Studio project containing a single DBML file, Entity classes for your database tables & helpful Query Extensions generated using CodeSmith Studio and PLINQO.
Let’s get started..
When we fire up CodeSmith Studio we get the Template Explorer window,expand Framework and see PLINQO installed out of the box.
I have expanded the CSharp folder and notice that it contains a bunch of .cst template files,PLINQO uses them to generate our objects.
We look at them in just a moment, first click the … button on SourceDatabase property, for the demo I am using a Northwind database, so let’s select it.
We can see that rest of the properties fill automagically,now lets take a look at each of them.
After the source database info, PLINQO needs to know the target language to generate the entities,location where the files will be generated and so on.
Now lets talk about the not so obvious ones.
- By default PLINQO generates 3 projects, a DAL project, a Dynamic Data website or web application project and a Test project.
The Data Project group tells the namespace for the data project,do we need to copy the template files to the target folder and type of queries it should generate,this has 2 options QueryExtensions and ManagerClasses, we will look them in a future post.
The Interface Project needs to know whether or not generate a Data web service file, type of project (Web application or Website) and the project name.
The Test project properties tell whether or not include a Test project in the solution and the name of the test project.
Let’s leave the default selected,specify the target Location and hit Generate,
This launches Visual Studio with a solution that contains our 3 projects.
To generate Entities and Queries we need to do one last step before we’re on our way.
Before we do that let’s go behind the scenes to see how PLINQO will generate the classes for us,so
Right click on Northwind.Data.csp file and select Manage Outputs.
For instance double-click on DBML will bring up a dialog where one of the things I can do is change the Ignore Pattern to exclude tables with a matching name that I don’t want or change the name of the base class for all my entities.
In Entities I can change the default location of the Entities output directory for instance, similarly in Queries I can change the default name of the Datacontext object that PLINQO generates.
Let’s leave them unchanged, Cancel the dialog and go back to the Solution Explorer.
Right-click the Northwind.Data.csp file again and this time select Generate Outputs.
PLINQO will then build the solution,generate Entities and Queries required to query the database.
The final output looks like.
Let’s open the generated Northwind.dbml.
Perfect, now let’s go to Northwind.UI project and fire up Default.aspx in the browser.
Nice so we got a fully working ASP.NET Dynamic Data web application project generated using PLINQO from a Northwind database.
In a future post we’ll see how PLINQO simplifies querying data with the help of some useful extension methods and few other tricks.
- Download this VS2010 Solution with Northwind database script (does not require CodeSmith Studio)