Generate source code

In this tutorial, we'll use your project to generate source code from it so we can use the mapped elements in our own programs.

Configuring and generating source code

Please follow the following steps to generate sourcecode from the project we've created in the previous tutorial. This tutorial will be for C# and will generate .NET 4.5.2 code. If you're more into VB.NET, you should select VB.NET instead of C#.

  • Start the LLBLGen Pro designer. Either use the Programs start menu, Windows search or double click the LLBLGenPro.exe in the installation folder to start the LLBLGen Pro designer.
  • To open our tutorial project, select it from the Recent Projects list on the Home Tab, or select File -> Open Project... from the main menu, or select the project from the File -> Recent Projects... menu.
  • To generate code, either press F7, or select the menu option Project -> Generate Source-code... The Generate Code dialog opens.
  • As we have just one model in the project, it will show one task, for the Entity Model. As this is the first time we're generating code, it's not currently valid and we have to edit the task. Do this by double-clicking the task or by clicking the Edit Selected Task Specifics button. The Code Generation Task Configuration dialog opens.
  • By default the view is the 'Simple' view, with just 1 tab. On this tab, General Settings, select either C# of VB.NET for Target language and select the .NET version of your choice in the Target Platform drop-down.
  • For Root namespace, specify Northwind.Tutorial
  • There are two template groups defined: SelfServicing and Adapter. For this tuturial, choose 'Adapter'.
  • For Adapter there is one preset defined, which is preselected, SD.Presets.Adapter.General, so select that.
  • For Destination root folder under Code file parameters you should specify a folder into which you want to generate the code. For this tutorial we'll use c:\temp\NorthwindTutorial\DAL
  • Click OK to close the configuration dialog.
  • We're now done setting everything up. LLBLGen Pro will store your settings in the project file after code generation so you don't have to set things up the next time you're generating code. Click Perform Tasks... to generate the sourcecode into the Destination root folder.

Compiling the generated Visual Studio projects

You now have either one Visual Studio project (if you chose SelfServicing) or two Visual Studio projects (if you chose Adapter) generated for you. To compile and use them in your code, please follow the following steps. If you have used the LLBLGen Pro designer from within Visual Studio, the generated projects have been added to your solution automatically.

  • Start Visual Studio.
  • In Visual Studio, load the two generated csproj files, the Root namespace.Model.csproj and the Root namespace.Persistence.csproj file, by opening them using File -> Open -> Project / Solution.
  • Save the solution created for you by Visual Studio.
  • By default the generated Visual Studio project(s) already reference the right LLBLGen Pro Runtime Framework assemblies. See Compiling the code for more information and also about referencing the assemblies from Nuget instead.
  • Compile the solution.

Using the generated code

Now that you have compiling source code, the next step is to use it. We'll setup a small console app which will retrieve the number of customers from the Customers table. This tutorial will assume you've chosen Adapter as the template group.

  • In Visual Studio, with your solution with the two generated Visual Studio projects, right-click the solution in Solution Explorer and select Add -> New Project.
  • Select Visual C# -> Console Application and give it a name. Specify a location and click OK.
  • In the Solution Explorer, right-click the project you just created and select Set as Startup project
  • Right-click the References node below the new project and select Add Reference
  • In the Add Reference dialog, on the left click Projects and check the checkboxes of both projects. Stay in this dialog for the next step
  • To the new console application, you also need to add a reference to the SD.LLBLGen.Pro.ORMSupportClasses.dll assembly. Do this by clicking Assemblies -> Extensions in the left tree and check the checkbox of the LLBLGen Pro ORM Support Classes Library. If you instead want to pull the assembly from Nuget, install the SD.LLBLGen.Pro.ORMSupportClasses package from Nuget.
  • Close the Add reference dialog.
  • Drag the app.config file from the Root namespace.Persistence project to your new console application project to copy it. This file contains the connection string.
  • Compile the solution. This should succeed.
  • Open the Program.cs class in the console application. We're going to add some code to retrieve the number of customers from the Customers table. Alter the Program class so it looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Northwind.Tutorial.DatabaseSpecific;
using Northwind.Tutorial.Linq;

namespace Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            using(var adapter = new DataAccessAdapter())
            {
                var metaData = new LinqMetaData(adapter);
                var count = metaData.Customer.Count();
                Console.WriteLine("Number of customers: {0}", count);
            }
        }
    }
}

The Root namespace you've specified when generating code can differ from the code above, in which case you have to correct the last using statement. Running the code above should give:

Number of customers: 91
Press any key to continue . . .