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 just one template group for Entity Framework so leave it at 'General'.
  • Select the preset with tasks you want to run to generate the source code. For Entity Framework v6 there are two choices: DbContext API and Code First. For this tutorial, we'll select SD.EntityFramework.v6 (Code First) to generate Code First mappings.
  • 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 two cs/vbproj projects generated for you, one with the entity classes and one with the DbContext derived class and mapping related files, be it either an EDMX file (if you chose the DbContext API preset) or a ModelBuilder class (if you chose the CodeFirst preset). 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 your IDE, be it Visual Studio or Jetbrains Rider.
  • In your IDE, 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 your IDE.
  • You have to add a nuget reference to the Entity Framework v6 package in the Root namespace.Persistence project. Do this by right-clicking the References node under the Root namespace.Persistence project and select Manage Nuget Packages.
  • Click the Browse tab in the Nuget GUI, and select Entity Framework. Be sure to select v6.1.3 or higher and click Install.
  • 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.

  • 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. Click OK
  • 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 and other descriptions for Entity Framework to work.
  • To the new console application, add a reference to the Entity Framework v6 package from nuget with the same steps as in the previous section.
  • 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;

namespace Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            using(var context = new NorthwindDataContext())
            {
                var count = context.Customers.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. 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 . . .