DatabaseExtensionsCheckAndUpdateKeysTEntity Method
This extension method is used to check for null keys or incomplete rows added by controls and
either remove them or update them based on the passed in delegate methods.
Namespace: EWSoftware.EntityFrameworkAssembly: EWSoftware.EntityFramework (in EWSoftware.EntityFramework.dll) Version: 2025.11.12.0
public static void CheckAndUpdateKeys<TEntity>(
this DbContext dataContext,
Func<TEntity, EntityState, bool> nullKeyCheck,
Action<TEntity, EntityState> updateKeys
)
where TEntity : class, INotifyPropertyChanged
<ExtensionAttribute>
Public Shared Sub CheckAndUpdateKeys(Of TEntity As {Class, INotifyPropertyChanged}) (
dataContext As DbContext,
nullKeyCheck As Func(Of TEntity, EntityState, Boolean),
updateKeys As Action(Of TEntity, EntityState)
)
public:
[ExtensionAttribute]
generic<typename TEntity>
where TEntity : ref class, INotifyPropertyChanged
static void CheckAndUpdateKeys(
DbContext^ dataContext,
Func<TEntity, EntityState, bool>^ nullKeyCheck,
Action<TEntity, EntityState>^ updateKeys
)
[<ExtensionAttribute>]
static member CheckAndUpdateKeys :
dataContext : DbContext *
nullKeyCheck : Func<'TEntity, EntityState, bool> *
updateKeys : Action<'TEntity, EntityState> -> unit when 'TEntity : not struct and INotifyPropertyChanged
- dataContext DbContext
- The data context to use
- nullKeyCheck FuncTEntity, EntityState, Boolean
- The function delegate to execute for null key/incomplete row checks. If
it returns true, the row is assumed to be an empty row and is detached. If it returns false or is
not specified, the updateKeys action delegate is called.
- updateKeys ActionTEntity, EntityState
- The action delegate to execute for each row that should be retained. This
is used to update any keys in the row if necessary. If not specified, no action is taken.
- TEntity
- The entity type
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).
In certain cases, some list controls like data grids add a temporary row for new additions.
If the row is left without saving changes or the changes are cancelled, the row is not always removed
and still exists when changes are saved. Because it has null keys or missing values, it should not
be kept. This extension method can be used to find such rows and remove them. It can also be used
to update keys or other fields in new or existing rows if necessary.
// If a list control added a row but left it incomplete, delete it. If not, update the parent key.
dataContext.CheckAndUpdateKeys<ChildTable>(
(entity, state) => state == EntityState.Added && entity.GroupId == null,
(entity, state) => entity.ParentId = parent.ParentId);