Entity Framework Extensions PreBulkSaveChanges

Description

The PreBulkSaveChanges event is raised as soon as the BulkSaveChanges method is called and no configuration or anything else is done before. It allows you to set some global configuration for BulkSaveChanges.

Example

The following example sets the CreatedDate for new customers and ModifiedDate property for existing customers.

EntityFrameworkManager.PreBulkSaveChanges = ctx =>
{
    var modifiedEntities = ctx.ChangeTracker.Entries().ToList();

    foreach (var change in modifiedEntities)
    {
        if (objectStateEntry.State == EntityState.Added)
        {
            change.CurrentValues["CreatedDate"] = DateTime.Now;
        }
        else if (objectStateEntry.State == EntityState.Modified)
        {
            change.CurrentValues["ModifiedDate"] = DateTime.Now;
        }
    }
};

Try it in EF Core | Try it in EF6

In the PreBulkSaveChanges event, the CreatedDate for new customers and ModifiedDate property for existing customers is set to DateTime.Now before the data is saved to the database.


Last updated: 2023-03-01
Author:


Contents