Database ExtensionsLoad By KeyTEntity Method
Load all entities of the given type using a stored procedure defined on the entity type using the
given key value(s).
Definition
Namespace: EWSoftware.EntityFramework
Assembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
An enumerable list of the TEntity entities matching the key values. Typically, there will only be one entity but there could be others if a non-primary key is used.
Assembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
C#
public static IEnumerable<TEntity> LoadByKey<TEntity>(
this DbContext dataContext,
params Object?[] parameters
)
where TEntity : class, new()
VB
<ExtensionAttribute>
Public Shared Function LoadByKey(Of TEntity As {Class, New}) (
dataContext As DbContext,
ParamArray parameters As Object()
) As IEnumerable(Of TEntity)C++
public:
[ExtensionAttribute]
generic<typename TEntity>
where TEntity : ref class, gcnew()
static IEnumerable<TEntity>^ LoadByKey(
DbContext^ dataContext,
... array<Object^>^ parameters
)F#
[<ExtensionAttribute>]
static member LoadByKey :
dataContext : DbContext *
parameters : Object[] -> IEnumerable<'TEntity> when 'TEntity : not struct, new()Parameters
- dataContext DbContext
- The data context to use when loading the entities
- parameters Object
- One or more parameter values that will be passed to the stored procedure. The parameter order must match the declared key order on the entity but does not have to match the parameter order in the stored procedure.
Type Parameters
- TEntity
- The entity type to load
Return Value
IEnumerableTEntityAn enumerable list of the TEntity entities matching the key values. Typically, there will only be one entity but there could be others if a non-primary key is used.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type DbContext. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).Remarks
The stored procedure name is determined by looking for the LoadByKeyStoredProcedureAttribute on the entity type. If the attribute is not present, the stored procedure name is assumed to be the same as the entity type name without the ResultSetSuffix property value. The stored procedure must have a parameter for each of the passed key values in parameters.
If the connection is not in an open state, it is opened temporarily while loading the entities. If change tracking is enabled on the data context, changes to the entities will be tracked. If not, they will not be tracked.
Example
C#
using var dataContext = new MyDbContext();
var assetInfo = dataContext.LoadByKey<Asset>(assetKey).Single();