Retrieving a Single Business Entity with GetEntity()

There are several overloads of the GetEntity() that allow you to retrieve a single entity.

GetEntity() returns a single entity and automatically stores a reference to the retrieved entity in the business object's Entity property.

GetEntity()

Entity Framework Only

  • Returns a business entity of the default EntityType by executing the specified object query

    protected virtual EntityType GetEntity(IQueryable<EntityType> objectQuery)
    

  • Returns a business entity of the specified type by executing the specified object query

    protected virtual T GetEntity<T>(IQueryable<T> objectQuery)
    			where T : classnew()

Example 1 - Entity Framework
In the following code sample GetEntity() is called passing an object query and returns a single OrderEntity object:

In C#:

public OrderEntity GetOrderByOrderID(int orderID)
{
	IQueryable<OrderEntity> query = from order in this.ObjectContext.OrderEntitySet
									where order.OrderID == orderID
									select order;
 
	return this.GetEntity(query);
}

And in VB .NET:

Public Function GetOrderByOrderID(ByVal orderID As IntegerAs OrderEntity
 
Dim query As IQueryable(Of OrderEntity) =
	From order In Me.ObjectContext.OrderEntitySet
	Where order.OrderID = orderID
	Select order
 
	Return Me.GetEntity(query)
 
End Function

MM .NET "Classic" Entities Only

  • Returns a new Business Entity of the default EntityType containing the results of the command string (based on the default command type) executed against the default database

    protected virtual EntityType GetEntity(string command)
    

  • Returns a new Business Entity of the default EntityType containing the results of the command string (based on the default command type) with parameters executed against the default database.

    protected virtual EntityType GetEntity(string command, params IDbDataParameter[] cmdParams)
    

  • Returns a new Business Entity containing the results of the command string (based on command type) executed against the default database.

    protected virtual EntityType GetEntity(string command, CommandType cmdType)
    

  • Returns a new Business Entity of the default EntityType containing the results of the command string (based on command type) executed against the specified database.

    protected virtual EntityType GetEntity(string command, string databaseKey, CommandType cmdType)
    

  • Returns a new Business Entity of the default EntityType containing the results of the command string (based on the default command type) executed against the specified database.

    protected virtual EntityType GetEntity(string command, string databaseKey)
    

  • Returns a new Business Entity of the default EntityType containing the results of the command string (based on the default command type) w/ parameters executed against the specified database.

    protected virtual EntityType GetEntity(string command, string databaseKey,
    			CommandType cmdType, params IDbDataParameter[] cmdParams)

  • Returns a new Business Entity of the default EntityType containing the results of the command string (based on the specified command type) w/ parameters executed against the default database.

    protected virtual EntityType GetEntity(string command, CommandType cmdType, params IDbDataParameter[] cmdParams)
    

  • Returns a new Business Entity of the default EntityType containing the results of the command string (based on the specified command type) w/ parameters executed against the specified database
    protected virtual EntityType GetEntity(string command, string databaseKey,
    			CommandType cmdType, params IDbDataParameter[] cmdParams)

Example 1 - Classic Entities
In the following code sample GetEntity() is called passing a stored procedure that accepts a single parameters and returns a single OrderEntity object. This assumes the business object's DefaultCommandType (usually specified at the ABusinessObject level) is set to CommandType.StoredProcedure:

In C#:

public OrderEntity GetOrderByOrderID(int orderID)
{         
	return this.GetEntity("OrdersSelectByPK"this.CreateParameter("@OrderID", orderID));
}

And in VB .NET:

Public Function GetOrderByOrderID(ByVal orderID As IntegerAs OrderEntity
	Return Me.GetEntity("OrdersSelectByPK"Me.CreateParameter("@OrderID", orderID))
End Function

Example 2 - Classic Entities
In the following code sample GetEntity() is called passing a dynamic SQL statement that accepts a single parameter and returns a single OrderEntity object. This assumes the business object's default command type (usually specified at the ABusinessObject level) is set to CommandType.Text:

In C#:

public OrderEntity GetOrderByOrderID(int orderID)
{
	return this.GetEntity("SELECT * FROM Orders WHERE OrderID = @OrderID"this.CreateParameter("@OrderID", orderID));
}

And in VB .NET:

Public Function GetOrderByOrderID(ByVal orderID As IntegerAs OrderEntity
	Return Me.GetEntity("SELECT * FROM Orders WHERE OrderID = @OrderID"Me.CreateParameter("@OrderID", orderID))
End Function

See also:
Retrieving Lists of Business Entities with GetEntityList()


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 06/29/18
Comment or report problem with topic