Step 4 - Creating Business Classes w/ the Business Generator
In this step, you use the MM .NET Business Layer Generator to generate the entity classes and other associated objects such as the business object controller, business rules, and default value classes. The Business Layer Generator uses the structure of tables in the database to create new business classes or update existing business classes.
Note: The term "Code First" can be misleading. Microsoft still intends that you can take an existing database and reverse-engineer entities from it (it would be a waste of time not to). Often, developers initially generate a set of entity classes from a database (if one exists), then from that point on, make change to entity classes and update the database from those classes.
- Note: You must run Visual Studio with Administrator privileges on your development machine to use the Business Layer Generator. This is because the BLG is using Visual Studio Automation to examine config files in your project and to generate code!
- From the Visual Studio main menu, select Tools | MM .NET Business Layer Generator. This launches the Business Layer Generator (BLG) with the Order System Business Objects EF Code First project selected:

- Select the One subfolder for each business object check box. This places each set of generated business classes in its own folder.
- Click the Next button. This displays the Specify the Code Generation Settings page:

In the Entity Object section:
- Select the Entity Framework check box.
- Uncheck the EDMX Generation check box.
- Select the Annotations option.
- Check the Generate Partial Classes check boxes if it isn't already selected.
This page allows you to specify code generation settings for the business object and associated business entity, business rule and default value classes. By default the generator uses database table names as the root portion of the class name. You can specify a Table Name Strip Prefix which removes a specific character string prefix from your database table names before deriving names for your new classes.
You can also specify a suffix or prefix for your classes and the generator will add this to the beginning/end of the class names (you can manually change any aspect of the name in the next wizard step). The check boxes at the top of each group box allow you to specify which classes you want to generate. For more information on these settings, check out the topic Using the MM .NET Business Layer Generator.
- Accept the wizard default settings and click Next to continue. At the top of the page, the Databases combo box displays a list of databases you have specified in your app.config file (you only have one in this project). It also displays the Connection String you specified in your app.config file:

The Business Layer Generator uses this connection string to connect to the specified database to retrieve business rule, defaults, and data access information. You can manually change this string to use different authentication or connect to a completely different database.
- Click the Get Data button to retrieve a list of tables, views, and stored procedures from your database.
In the Tree View, three root nodes are displayed: Tables, Views, and Stored Procedures. You can generate business classes from any of these database objects. For this example you will generate from Northwind tables. Expand the Tables node by clicking on the plus sign (+) next to it. You may also want to resize the height of the dialog so you can see more information.
Select to generate business classes from the tables shown in the following image (Note: A warning dialog appears when selecting some tables that have foreign keys pointing to other tables. You can ignore the warning, since we are eventually selecting all related tables):

Note: To make the data retrieval process much faster, the Business Layer Generator does not retrieve column information from a database object (table, view, or stored procedure) until you first select the node.As you select each table the Business Object Class Name, Entity Class Name, Business Rule Class Name, and Default Values Class Name text boxes display names of the classes to be generated based on the settings specified in the previous wizard page. These class names are just defaults that can be overridden by manually changing the names in the text boxes.
Here's information on each of the check boxes:
- When the Intelligent Renaming check box is selected, changing the root portion of the Business Object Class Name automatically changes the names of the other objects accordingly.
- When the Singularize check box is selected,table names that are plural are automatically changed to singular. For example, the Categories table produces an entity named Category.
- When the String Min/Max check box is selected, the BLG generates minimum and maximum rules for entity string properties.
- When the Indexes check box is selected, the BLG generates index information for entities.
- When the Foreign Keys check box is selected, the BLG generates foreign key information for entities.
- In addition to specifying class-level settings, you can also specify information regarding properties of entity objects. To see an example of this, expand the Orders node in the tree view and select the CustomerID column. This displays Entity Properties settings to the right of the tree view for the currently selected item:

- Property Name specifies the property name that will be generated for the selected data column.
- Display Name specifies the "friendly name" used when displaying messages associated with this data column (for example, in business rule messages).
- The Key check box is automatically checked if the selected table column is a primary key.
- If the Required check box is checked, a "required field" business rule is generated for the selected column. By default, if a data column is NOT specified as nullable, this check box is checked. In addition, the check box is disabled for auto-generated primary key data columns (such as identity columns).
- The Set Default check box and associated text box allow you to specify default value information for new records (more on this in a just a bit).
- Since the CustomerID column is a string type, nchar(5), the Min control shows the minimum length of the string is zero (0), and the Max length of the string is 5.
- Sadly, the CustomerID and ShipName columns are marked as nullable--a bad choice. Normally, you should go back to the database and specify these columns as not nullable. However, in some cases, you may not have control of the database you are working with.
As shown in the following steps you can still mark these columns as required in the BLG. If you have control of the database, you can later update the database from the entities and the corresponding table columns will be marked as non-nullable.
- Mark the CustomerID column as Required. This displays an Allow Empty Value check box. Leave this unchecked since we don't want to allow an empty string value in this column.
- Select the ShipName column in the tree view and also mark it as Required.
- Now collapse the Orders node by clicking on the - sign, then expand the Order Details node and select its OrderID column.
The Key check box is selected, indicating this column is part of a primary key. You can also see this information in the Tables tree view, where it shows the column is both a primary key (PK) and foreign key (FK), but not an Identity column (as is the Orders table's OrderID column shown in the image above).
Notice the ProductID column is also a primary key and a foreign key. This means the Order Details table has a composite primary key comprised of the OrderID and ProductID columns. We'll later see how this affects the generation of the associated OrderDetailEntity class:

- With the OrderID column still selected, notice the Set Default check box and text boxes are visible. This only happens when you select a primary key that is not an Identity column, since in that case, the database generates a value for you.
The Set Default check box is unselected by default for a non-Identity primary key. Select the Default Value check box, but leave the default values text box empty, without a hard-coded value. This allows the default value to be specified dynamically at run time.
This comes in handy when adding Order Detail items to an Order at runtime, where you need to specify the OrderID of the Order the detail item belongs to.
- Now select the Quantity column in the tree view. Notice the Set Default check box is already selected:

This is because the Business Layer Generator automatically generates default values for non-nullable, value-type required fields. Also, notice the number 1 is displayed as the default value. This value was derived from the Northwind database default value constraints. It can be changed in the Business Layer Generator if so desired. In this example, let's leave the default value set to 1. This is a hard-coded value that will not vary at run time.
- Next, select the Discount column. Notice the Set Default and Required check boxes are already selected, and the default value is set to (0). Select the Allow Empty Value check box. This indicates that a Discount value of zero (0) is allowed:

- Click Next to continue. This displays the Data Access Layer Options page of the wizard. By default, the Use Dynamic SQL Statements options is selected (or if you previously ran the generator, this page defaults to the previous setting you used):

For now, select the Use Dynamic SQL Statements option and we will examine the other options later on in the Jump Start. This option specifies that insert, update, and delete statements will be dynamically generated by the Entity Framework and MM .NET at run time, so there is no need to create a custom data access class.
- Click Next to continue. This displays the Ready to Generate page of the wizard. The Summary box displays the target business object project and the number of business objects to be generated. The option Overwrite Existing Files is selected automatically (the BLG remembers this setting from the last time you ran it):

- Click Finish to generate the business object classes. When code generation is complete the following dialog is displayed:

- Click OK to close the dialog. Your business object project should look something like this:

See Also:
Step 5 - Examining the Generated Code
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 02/10/26
Comment or report problem with topic
