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.

Definition

Namespace: EWSoftware.EntityFramework
Assembly: 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

Parameters

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.

Type Parameters

TEntity
The entity type

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

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.

Example

C#
// 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);

See Also