Reverse engineer entities from a database
The LLBLGen Pro Designer allows you to work database first (entity definitions are reverse engineered from table or view definitions) and model first (table definitions are created from entity definitions), and you can choose between them at any given time. For this tutorial, you'll use the database first functionality of the designer by reverse engineering an set of tables into entity definitions, complete with mappings.
For this tutorial it's required you have an existing database with tables available to reverse engineer. This tutorial uses SQL Server and the example database 'Northwind' but it's the same for all other databases supported by the LLBLGen Pro designer.
Retrieving relational model data
- 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.
- Right-click the Relational Model Data node in the Project Explorer and select Add Relational Model Data from a Database. The wizard starts.
- Select the database driver matching the database type to connect to. For this tutorial we'll connect to SQL Server and we therefore select the SQL Server 2000-2016 Driver (SqlClient).
- Specify the authentication information. If you're working locally and you're running the designer as administrator, you could leave WindowsAuthentication selected for Authentication Type. If you want to connect to a remote server instance, it might be you need to specify a database account, e.g. sa and its password.
- Specify the server name. For server name, specify for example myserver.domain.com or if you're working locally on a SqlExpress installation, you probably need to use .\SQLEXPRESS
- Click Next.
- The SQL Server driver will now obtain all catalogs (databases) in the SQL Server instance you connected to as well as the schemas. If you expand a node the driver will retrieve the tables, views, table valued functions and stored procedures, to which the user you used to connect with as access.
- We're creating a project on Northwind, so we'll check the checkbox in front of Northwind in the list. This will check the 'dbo' schema and all tables, stored procedures and views in Northwind. Click Next.
- The wizard shows the stored procedures found. Check the checkbox in front of SalesByYear and click Next and Finish.
- LLBLGen Pro now knows enough information to obtain the meta-data for the project and will retrieve the meta data for the specified catalogs, in this case Northwind, from the database server.
-
If you open the Catalog Explorer (which is might be hidden away in a tab at the
right edge of the LLBLGen Pro designer. Just click the tab to make it
slide into view), you can browse through the meta-data retrieved by the designer.
When you click schemas, tables or their subnodes, press
Ctrl-Shift-D
or right-click and select Show Details to open a tab with additional meta-information of the node(s) currently selected.
You now have an LLBLGen Pro project with all the relational model data from the catalog(s) you specified, in this case Northwind, so you can design the entities without having to be connected to the database server.
Reverse engineering entities from tables
It's now time to do something useful with this meta data: map entities and other elements onto the tables and views!
- To create entities mapped onto tables, right-click in Catalog Explorer on the 'Northwind' node and select 'Reverse Engineer Tables to Entity Definitions' from the context menu.
- The dialog to select which entities to create opens, showing all database tables in the catalog and entity names of the entities to map on these tables. If the checkbox of an element isn't checked, use the Checking and Selecting tools in the lower left corner of the dialog to select all rows and check the checkboxes of the selected rows. You can rename entity names here if you want to and assign a groupname to one or more elements if needed. When everything looks OK, click Add to Project.
- In the Project Explorer, all entities you've added are now present. To view all added entities to your project, please expand the Entities node below the folder icon in Project Explorer. LLBLGen Pro automatically detected the relationships between the entities you've specified (if there were any FK constraints defined in the database, and Northwind has these defined). All fields are mapped by default to a table field. Please expand some entity nodes to see that the entities indeed are filled with fields and relations.
-
Save your work. It's now a good time to save the project. Click
File -> Save Project (or press
Ctrl-S
) to save the project to the Location specified when the project was created. -
To examine an entity in detail, **right-click the entity in the
Project Explorer and select Edit. This will open the Entity Editor.
You can also double-click the entity or press
Ctrl-Shift-O
while the entity is selected in the Project Explorer. Check the Field mappings tab at the bottom of the entity editor to see the mapping created by the designer in the reverse engineering process.
Validating your project
The LLBLGen Pro Designer will validate your project in real time in the background. If it detects an error or warning, it will notify you in the Errors & Warnings
pane docked at the bottom. You can also validate the project manually by selecting from the menu Project -> Validate or by pressing F6
.
If you run into an error, you can expand the error in the Errors & Warnings pane to show the suggestions and quick-fixes the designer gives you.
- Click the Errors & Warnings pane to make it visible if it's docked away.
- It's likely there's an error, like the following: "The target framework specific setting 'ProviderManifestTokenValue' hasn't been set to a valid value. This value is required by the Entity Framework at runtime."
- Expand this error in the Errors & Warnings pane by clicking the
[+]
button in front of it. - The error has a correction/suggestion node, which says: "Choice 1: Open the Project Settings and click the 'Entity Framework' node in the tree at the left. At the right, specify a value for the setting 'ProviderManifestTokenValue'"
- Each suggestion has a handy link to go to the source of the error/warning, visible as the Go there link. Click the Go there link.
-
It opens the Project settings dialog. Navigate to Conventions->Entity Model->Entity Framework v6 and click Entity Framework v6. On the right, in the
Provider manifest token setting, specify the database token, e.g.
2008
or2012
. - Click OK to close the settings dialog.
- Go back to Errors & Warnings and verify the error is now gone.
Next step
The next step is generating source code from your project and using it in a program: Generate source code