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's one template group for Linq to SQL so leave it at 'General'.
  • There's one preset defined for Linq to SQL, SD.LinqToSql, which is pre-selected.
  • 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 cs/vbproj projects

You now have one cs/vbproj project generated for you, with the entity classes and the DataContext derived class and mapping related files. 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 project has been added to your solution automatically.

  • Start your IDE, be it Visual Studio or Jetbrains Rider.
  • In your IDE, load the generated csproj file, the Root namespace.csproj, by opening them using File -> Open -> Project / Solution.
  • Save the solution created for you by your IDE.
  • The Linq to SQL assemblies ship with .NET, so you can directly compile the solution and use it in your code.

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.

  • In Visual Studio, with your solution with the generated Visual Studio project, 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 checkboxe of the generated project. Click OK
  • Drag the app.config file from the Root namespace project to your new console application project to copy it. This file contains the connection string for Linq to SQL to work.
  • To the new console application, add a reference to System.Data.Linq by right-clicking References and selecting Add reference from the context menu. Then from the tree on the left, click Assemblies and select Framework and then check the checkbox in front of System.Data.Linq. Click OK.
  • 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 the following. The code below shows the usage of generated code using an xml mapping file. If you use attribute based mappings you can just instantiate the DataContext without reading the xml file.
using System;
using System.Collections.Generic;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Northwind.Tutorial;

namespace Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            using(var context = GetContext())
            {
                var count = context.Customers.Count();
                Console.WriteLine("Number of customers: {0}", count);
            }
        }

        private static NorthwindDataContext GetContext()
        {
            var modelAssembly = typeof(NorthwindDataContext).Assembly;
            var resourceStream =
                modelAssembly.GetManifestResourceStream("Northwind.Tutorial.NorthwindMappings.xml");
            return new NorthwindDataContext(XmlMappingSource.FromStream(resourceStream));
        }
    }
}

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. It also assumes the generated datacontext is named NorthwindDataContext. If you named your project differently, correct it to the generated DataContext class in the Root namespace.Persistence project. Running the code above should give:

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