Retrieving Entities and Entity Lists
One of the great benefits of using the Entity Framework with MM .NET is the ability to use Language Integraged Queries (LINQ).
Retrieving a Single Entity Using LINQ
To retrieve a single entity using LINQ and the Entity Framework, you can call your business object's GetEntity() method.For example, in C#:
public EmployeeEntity GetEmployeeByID(int employeeID)
{
IQueryable<EmployeeEntity> query = from e in this.ObjectContext.EmployeeEntitySet
where e.EmployeeID == employeeID
select e;
return this.GetEntity(query);
}
And in VB .NET:
Public Function GetEmployeeByID(ByVal employeeID As Integer) As EmployeeEntity
Dim query As IQueryable(Of EmployeeEntity) = From e In Me.ObjectContext.EmployeeEntitySet _
Where e.EmployeeID = employeeID _
Select e
Return Me.GetEntity(query)
End Function
The first line of code creates a LINQ query from the associated Object Context. It then passes it to the MM .NET GetEntity() method which executes the query and returns a single entity.
Lazy Loading
If you have an entity object that has a relationship defined in the EDMX to another entity or list of entities, by default the associated entity/entities are not loaded. For example, in the following diagram, UserEntity has a relationship to the PersonEntity:
This "lazy loading" is efficient because you don't automatically return entities that you don't need. However, if you would like to load the entity/entities afterwards, you can use the Load() method on the entity reference.
For example, in C#:
// Instantiate the business objects
User user = new User();
UserEntity entity = user.GetUserByPK(1);
// Loads the Person entity reference
entity.PersonReference.Load();
And in VB .NET:
' Instantiate the business objects
Dim user As User = New User()
Dim entity As UserEntity = user.GetUserByPK(1)
' Loads the Person entity reference
entity.PersonReference.Load()
Eager Loading
If you have an entity object that has a relationship and you want to load the associated entity or entity list when you do your initial data retrieval (eager loading), you can do so by using the Include() method. The Include() method accepts the name of the navigation property you want to include in the data retrieval. So, if the UserEntity has a reference property named Person that refers to a PersonEntity, you can do the following.In C#:
IQueryable<UserEntity> query =
from u in this.ObjectContext.UserEntitySet.Include("Person")
where u.UserPK == userPK
select u;
And in VB .NET:
Dim query As IQueryable(Of UserEntity) = _
From u In Me.ObjectContext.UserEntitySet.Include("Person") _
Where u.UserPK = userPK _
Select u
You should only use the Include() method in your data retrieval when you almost always need access to the other data. Otherwise, it's more efficient to do lazy loading as described in the previous section.
Retrieving a List of Entities Using LINQ
To retrieve a list of entities using LINQ and the Entity Framework, you can call your business object's GetEntityList() method.For example, in C#:
public mmBindingList<EmployeeEntity> GetEmployeesByRegion(string region)
{
IQueryable<EmployeeEntity> query = from e in this.ObjectContext.EmployeeEntitySet
where e.Region == "WA"
select e;
return this.GetEntityList(query);
}
And in VB .NET:
Public Function GetEmployeesByRegion(ByVal region As String) As mmBindingList(Of EmployeeEntity)
Dim query As IQueryable(Of EmployeeEntity) = From e In Me.ObjectContext.EmployeeEntitySet _
Where e.Region = "WA" _
Select e
Return Me.GetEntityList(query)
End Function
The first line of code creates a LINQ query from the associated Object Context. It then passes it to the MM .NET GetEntityList() method which executes the query and returns a list of entities.
Retrieving a Single Entity Using a Stored Procedure
In order to use a stored procedure to retrieve an entity, you must first add it to the Entity Data Model as described in the topic Adding Stored Procedures that Retrieve Entities.After adding the stored procedure to the Entity Data Model, it becomes a method on your business object's Object Context. You can then call GetEntity(), passing the results of calling the object context method.
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
Retrieving a List of Entities Using a Stored Procedure
In order to use a stored procedure to retrieve a list of entities, you must first add it to the Entity Data Model as described in the topic Adding Stored Procedures that Retrieve Entities.After adding the stored procedure to the Entity Data Model, it becomes a method on your business object's Object Context. You can then call GetEntityList(), passing the results of calling the object context method.
For example, in C#:
public mmBindingList<OrderEntity> GetOrdersByCustomerID(string customerID)
{
return this.GetEntityList(this.ObjectContext.OrdersSelectByCustomerID(customerID));
}
And in VB .NET:
Public Function GetOrdersByCustomerID(ByVal customerID As String) As mmBindingList(Of OrderEntity)
Return Me.GetEntityList(Me.ObjectContext.OrdersSelectByCustomerID(customerID))
End Function
Retrieving All Entities
To retrieve all entities of a particular type you can call your business object's GetAllEntities() method, and MM .NET will create the query for you:In C#:
Employee e = new Employee();
mmBindingList<EmployeeEntity> employeeList = e.GetAllEntities();
And in VB .NET:
Dim e As Employee = New Employee()
Dim employeeList As mmBindingList(Of EmployeeEntity) = e.GetAllEntities()
You don't need to create a custom method to retrieve all entities of a particular type. Just call the business object's built-in GetAllEntities() method and MM .NET automatically generates the proper query for you and returns all entities of the default type.
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/26/18
Comment or report problem with topic
