Moving a Classic Entity Business Object to Entity Framework

If you have an existing project that uses "Classic" MM .NET entities, this topic shows how to move it to Entity Framework.

  1. Add an Entity Data Model to your project, if this is the first entity you are converting to the Entity Framework.


  2. Next, add entities to your entity data model. For details, see the topic Adding Entities to an Entity Data Model. There are some important instructions in this step, so we recommend reading this section carefully.

  3. If you were to rebuild your project now (hold off for just a bit) you would have duplicate copies of your entity classes--the original classic MM .NET entity classes, and the entity classes generated by the Entity Data Model. To fix this problem, you could manually remove all properties from your entity object. However, it's best to let the MM .NET Business Layer Generator do this for you since running the BLG will also set up other important properties and methods on your business objects.

    For details, see the topic Creating Business Classes with the Business Layer Generator.

  4. Now, you can examine the results. First of all, take a look at the changes to the main Business Object Controller class.

    Entity Property

    If you open the class in the code editor and expand the Association Properties region, you will see the Entity property has been changed, so there is no longer a reference to the entity's DataRow (since there is no longer any underlying DataRow with the Entity Framework).

    For example, in C#:

    /// <summary>
    /// Business Entity object
    /// </summary>
    public override EmployeeEntity Entity
    {
        get
        {
            if (this._entity == null)
            {
                this._entity = this.CreateEntityObject();
            }
            return this._entity;
        }
        set
        {
            this._entity = value;
        }
    }
    private EmployeeEntity _entity;

    And in VB .NET:

    '''<summary>
    ''' Business Entity object
    ''' </summary>
    Public Overrides Property Entity() As EmployeeEntity
        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 EmployeeEntity

    ObjectContext Property

    There is also a new ObjectContext property that is the same type as the Object Context class generated by the Entity Data Model.

    For example, in C#:

    /// <summary>
    /// Object Context Property
    /// </summary>
    public NorthwindEntitiesContainer ObjectContext
    {
        get
        {
            return (NorthwindEntitiesContainer)this.ObjectContextEF;
        }
        set
        {
            this.ObjectContextEF = value;
        }
    }

    And in VB .NET:

    '''<summary>
    ''' Object Context Property
    ''' </summary>
    Public Property ObjectContext() As NorthwindEntitiesContainer
        Get
            Return CType(Me.ObjectContextEF, NorthwindEntitiesContainer)
        End Get
        Set
            Me.ObjectContextEF = value
        End Set
    End Property

    DataAccessObject Property

    If you previously had a custom data access class associated with this business object, the DataAccessObject property is no longer there. It's not necessary since you can use a generic MM .NET Entity Framework data access class for inserts, updates, and deletes.

    CreateDataAccessObject() Method

    If you previously had a CreateDataAccessObject() method that instantiated a custom data access class, this method is no longer there.

    Business Object Constructor

    In the Business Object constructor, there is new line of code that sets the business object's EntityFramework property to true.

    The Entity Class

    If you open the Entity Class, you should that all entity properties have been removed.

    For example, in C#:

    /// <summary>
    /// Summary description for EmployeeEntity.
    /// </summary>
    public partial class EmployeeEntity : ABusinessEntity
    {
     
    }

    And in VB .NET:

    ''' <summary>
    ''' Summary description for EmployeeEntity
    ''' </summary>
    Partial Public Class EmployeeEntity
        Inherits ABusinessEntity
     
     
    End Class

  5. These final steps are the biggest (although relatively painless) and involve changing your business object methods.

    If you are using C#, you should add the following using statements to the top of all business object controller classes you want to move to the Entity Framework:

    using System.Linq;
    using System.Data.Objects;

    If you are using VB .NET, you can simply import these namespaces at the project level. To do this, double-click your project's My Project node, and in the Project Properties panel, select the References tab. At the bottom of the page under Imported namespaces, add a reference to the following namespaces:

    • System.Linq
    • System.Data.Objects

    This makes these namespaces available to all source code in the project.

  6. Every change we have looked at so far was made automatically for you by the Business Layer Generator. However, when it comes to the code in your business object methods, you need to make these changes manually. That said, these are usually simple changes. Normally, you only have to change the code that calls the GetEntity() or GetEntityList() methods that retrieve entities.

    Changing Your Business Object Methods to Use LINQ

    If your existing business object methods use dynamic SQL to retrieve entities, it makes sense to change them to use LINQ instead. Remember, that rather than performing queries against the back end database directly, you are performing queries against the Entity Data Model. These queries are translated by the Entity Framework to proper SQL statements for the back end database your application is using.

    So, for example, the following method

    In C#:

    public mmBindingList<OrderEntity> GetOrdersByCustomerID(string customerID)
    {
        return this.GetEntityList("OrdersSelectByCustomerID", this.CreateParameter("@CustomerID", customerID));
    }

    And in VB .NET:

    Public Function GetOrdersByCustomerID(ByVal customerID As String) As mmBindingList(Of OrderEntity)
     
        Return Me.GetEntityList("OrdersSelectByCustomerID", Me.CreateParameter("@CustomerID", customerID))
     
    End Function

    Can be changed to the following LINQ query.

    In C#:

    public mmBindingList<OrderEntity> GetOrdersByCustomerID(string customerID)
    {
        IQueryable<OrderEntity> query = from o in this.ObjectContext.OrderEntitySet
                                        where o.CustomerID == customerID
                                        select o;
     
        return this.GetEntityList(query);
    }

    And in VB .NET:

    Public Function GetOrdersByCustomerID(ByVal customerID As String) As mmBindingList(Of OrderEntity)
     
        Dim query As IQueryable(Of OrderEntity) = From o In Me.ObjectContext.OrderEntitySet _
         Where (o.CustomerID = customerID) _
         Select o
     
        Return Me.GetEntityList(query)
     
    End Function

    Changing Your Business Object Methods to Use Stored Procedures

    If your existing business object methods use stored procedures to retrieve entities, you can still change them to use LINQ instead. However, LINQ queries ultimately get translated to dynamic SQL on the back end, which may not be acceptable in some development shops. So, if you prefer to continue using stored procedures, you can do so by following the steps outlined in this section.

    In this example, the following classic MM .NET method using a store procedure will be converted to a method using the same stored procedure with the Entity Framework.

    In C#:

    return this.GetEntity("OrdersSelectByPK", this.CreateParameter("@OrderID", orderID));

    And in VB .NET:

    Return Me.GetEntity("OrdersSelectByPK", Me.CreateParameter("@OrderID", orderID))

    For details on using a stored procedure to retrieve entities, check out the topic Adding Stored Procedures to an Entity Data Model for details.

    Once you have added the stored procedure for retrieving one or more entities, you can call it as a method on the Object Context class.

    For example, in C#:

    public OrderEntity GetOrderByOrderID(int orderID)
    {
        return this.GetEntity(this.ObjectContext.OrdersSelectByPK(orderID));
    }

    And in VB .NET:

    Public Function GetOrderByOrderID(ByVal orderID As Integer) As OrderEntity
        Return Me.GetEntity(Me.ObjectContext.OrdersSelectByPK(orderID))
    End Function




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