Step 5 : Examining the Generated Code

Examining the Generated Business Objects

In the Solution Explorer, click the Order.vb file to open it in the code editing window. Your source code should look something like this:

''' <summary>
''' Summary description for Order
''' </summary>
Partial Public Class Order
	Inherits ABusinessObject(Of OrderEntity)
 
 
#Region "Association Properties"
 
	'''<summary>
	''' Business Entity object
	''' </summary>
	Public Overrides Property Entity() As OrderEntity
		Get
			If Me._entity Is Nothing Then
				Me._entity = Me.CreateEntityObject()
			End If
			Return Me._entity
		End Get
		Set
			Me._entity = Value
		End Set
	End Property
	Private _entity As OrderEntity
 
	''' <summary>
	''' Business Rule object
	''' </summary>
	Public Overridable Property Rules As OrderRules
		Get
			Return CType(Me.BusinessRuleObj, OrderRules)
		End Get
		Set
			Me.BusinessRuleObj = Value
		End Set
	End Property
 
#End Region
 
	''' <summary>
	''' Constructor
	''' </summary>
	Public Sub New()
		Me.EntityFramework = True
		Me.TableName = "DboOrders"
		Me.PhysicalDbcObjectName = "dbo.Orders"
		Me.PrimaryKey = "OrderID"
		Me.HookConstructor()
		Me.EntityCentric = True
	End Sub
 
	''' <summary>
	''' Factory method that creates a business rule object
	''' </summary>
	''' <returns>Reference to the business rule object</returns>
	Protected Overrides Function CreateBusinessRuleObject() As mmBusinessRule
		Return New OrderRules(Me)
	End Function
 
	''' <summary>
	''' Set default values on the new entity
	''' </summary>
	''' <param name="entity">New Entity</param>
	Protected Overrides Sub HookSetDefaultValues(entity As OrderEntity)
 
		'' Store the hard-coded default values via the entity object
		If Not (entity Is NothingThen
			entity.Freight = 0
		End If
	End Sub
End Class

Things to note:

  • The namespace of the Order business object (Acme.OrderSystem.BusinessCF) is the default namespace you specified when first creating the project.

  • The name of the business class (Order) is what you specified in the MM .NET Business Layer Generator.

  • The base class of the Order business object is ABusinessObject.

  • If you expand the Association Properties region by clicking on the + sign to its right you can see association properties have been created for the associated Entity and Rules objects. You can also view the contents of a collapsed region by simply hovering your mouse pointer over it.

  • The first line of constructor code sets the EntityFramework property to true, indicating that by default, the business controller object works with Entity Framework entities.

  • The second line of constructor code stores the value "Orders" in the business object's TableName property. In a pure Entity Framework implementation, this property is not used. In mixed-mode implementations where you use both the Entity Framework along with classic MM .NET entity objects, this property specifies the default table name when the business object retrieves data into an ADO.NET DataSet.

  • The fourth line of constructor code stores the value "OrderID" into the object's PrimaryKey property. In a pure Entity Framework implementation, this property is not used. In mixed-mode implemetations where you use both the Entity Framework along with classic MM .NET entity objects, this property specifies which column in the associated DataTable holds the primary key. It was automatically derived by the Business Layer Generator from the Northwind database.

  • CreateBusinessRuleObject() is a factory method used at run time to instantiate the OrderRules object.

  • The HookSetDefaultValues() method is called on a new entity when you run the business object's CreateEntity() method. It stores a zero (0) default value to the entity's Freight property just as specified in the Business Layer Generator.

Examining the Generated Entities

Now, let's take a look at some of the generated entities.

  1. In the Solution Explorer, select the OrderEntity.vb file, to see the generated code:

    ''' <summary>
    ''' Summary description for OrderEntity
    ''' </summary>
    <Table("Orders")>
    Partial Public Class OrderEntity
    	Inherits ABusinessEntity
    	<Key>
    	Public Property OrderID As Integer
     
    	<Column(TypeName:="nchar")>
    	<ForeignKey("Customer")>
    	<StringLength(5)>
    	Public Property CustomerID As String
     
    	<ForeignKey("Employee")>
    	Public Property EmployeeID As Nullable(Of Integer)
     
    	Public Property OrderDate As Nullable(Of DateTime)
     
    	Public Property RequiredDate As Nullable(Of DateTime)
     
    	Public Property ShippedDate As Nullable(Of DateTime)
     
    	<ForeignKey("Shipper")>
    	Public Property ShipVia As Nullable(Of Integer)
     
    	Public Property Freight As Nullable(Of Decimal)
     
    	<StringLength(40)>
    	Public Property ShipName As String
     
    	<StringLength(60)>
    	Public Property ShipAddress As String
     
    	<StringLength(15)>
    	Public Property ShipCity As String
     
    	<StringLength(15)>
    	Public Property ShipRegion As String
     
    	<StringLength(10)>
    	Public Property ShipPostalCode As String
     
    	<StringLength(15)>
    	Public Property ShipCountry As String
     
    	'' Relationships
    	Public Overridable Property Customer As CustomerEntity
    	Public Overridable Property Employee As EmployeeEntity
    	Public Overridable Property Shipper As ShipperEntity
     
    End Class

    A few things to note:

    • At the top of the class, the <Table("Orders")> annotation specifies the name of the associated table. When forward engineering, EF will create a table in the database named Orders from this entity.

    • The <Key> annotation specifies OrderID is the primary key.


      Note: Entity Framework Code First requires that every table has a primary key!

    • The <Column(TypeName:="nchar")> and <StringLength(5)> annotations on the CustomerID property specify that the corresponding table column is nchar(5). Properties only need a TypeName annotation when the specific type can't be derived from the type of the entity property.

    • The <ForeignKey("Customer")> annotation on the CustomerID property indicates it is a foreign key, and the associated navigation property is called Customer, which you can see at the bottom of the class declaration:

      '' Relationships
      Public Overridable Property Customer As CustomerEntity
      Public Overridable Property Employee As EmployeeEntity
      Public Overridable Property Shipper As ShipperEntity

    • The <Required> annotation indicates the CustomerID and ShipName are required values.

  2. Next, in the Solution Explorer, select the OrderDetailEntity.vb file to see the generated code:

    ''' <summary>
    ''' Summary description for OrderDetailEntity
    ''' </summary>
    <Table("OrderDetails")>
    Partial Public Class OrderDetailEntity
    	Inherits ABusinessEntity
    	<KeyColumn(Order:=1)>
    	Public Property OrderID As Integer
     
    	<KeyColumn(Order:=2)>
    	Public Property ProductID As Integer
     
    	<Required>
    	Public Property UnitPrice As Decimal
     
    	<Required>
    	Public Property Quantity As Int16
     
    	<Required>
    	Public Property Discount As Single
     
     
     
    End Class

    Because the OrderDetails table has a composite primary key, there are <Key, Column(Order:=1)> and <Key, Column(Order:=2)> annotations on the OrderID and ProductID properties, indicating the order of the key columns.

  3. In the Solution Explorer, click on the EntityDataModelContainer.vb file, and check out the OnModelCreating() method:

    ''' <summary>
    ''' OnModelCreating method
    ''' </summary>
    ''' <param name="modelBuilder"></param>
    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
    	'' Remove the following line of code if you want to generate index on FKs
    	modelBuilder.Conventions.Remove(Of ForeignKeyIndexConvention)()
     
    	MyBase.OnModelCreating(modelBuilder)
     
    End Sub

    This method is called at run time the first time an instance is created from the EntityDataModelContainer class. It provides a place for you to add code that further configures the Entity Data Model.

    Examining the DbSets

    In addition to generating individual entities for each table, the MM .NET BLG also adds DbSet properties to your project's DbContext.

    1. With the EntityDataModelContainer.vb file still selected, notice the DbSet properties at the top of the class file. One property is created for each table, and is used to retrieve entities of that type from the database:

      Public Class EntityDataModelContainer
      	Inherits DbContext
       
      #Region "ObjectSet Properties"
       
      	''' <summary>
      	''' SupplierEntities DbSet
      	''' </summary>
      	Public Overridable Property SupplierEntities() As DbSet(Of SupplierEntity)
       
      	''' <summary>
      	''' ShipperEntities DbSet
      	''' </summary>
      	Public Overridable Property ShipperEntities() As DbSet(Of ShipperEntity)
       
      	''' <summary>
      	''' RegionEntities DbSet
      	''' </summary>
      	Public Overridable Property RegionEntities() As DbSet(Of RegionEntity)
       
      	''' <summary>
      	''' ProductEntities DbSet
      	''' </summary>
      	Public Overridable Property ProductEntities() As DbSet(Of ProductEntity)
       
      	''' <summary>
      	''' OrderEntities DbSet
      	''' </summary>
      	Public Overridable Property OrderEntities() As DbSet(Of OrderEntity)
       
      	''' <summary>
      	''' OrderDetailEntities DbSet
      	''' </summary>
      	Public Overridable Property OrderDetailEntities() As DbSet(Of OrderDetailEntity)
       
      	''' <summary>
      	''' EmployeeEntities DbSet
      	''' </summary>
      	Public Overridable Property EmployeeEntities() As DbSet(Of EmployeeEntity)
       
      	''' <summary>
      	''' CustomerEntities DbSet
      	''' </summary>
      	Public Overridable Property CustomerEntities() As DbSet(Of CustomerEntity)
       
      	''' <summary>
      	''' CategoryEntities DbSet
      	''' </summary>
      	Public Overridable Property CategoryEntities() As DbSet(Of CategoryEntity)
       
      #End Region
      

    Step 7 : Updating the Database


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 02/12/26
Comment or report problem with topic