Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create extension method #56

Closed
Inlustris opened this issue Jun 26, 2018 · 2 comments
Closed

Create extension method #56

Inlustris opened this issue Jun 26, 2018 · 2 comments
Labels

Comments

@Inlustris
Copy link

I'm trying to create an override for the BulkInsert function. In my dbcontext I use an override on SaveChanges() where i edit several properties of the object and return the base.SaveChanges();

Is it possible to create this for the bulklinsert?

@borisdj
Copy link
Owner

borisdj commented Jun 26, 2018

BulkOps are not calling SaveChanges() method so overriding it won't have any effect.
You could override extension methods, I use that for setting AuditInfo.
Example:

public static class MyContextBulkExtensions
{
    public static Task BulkInsertAsync<T>(this CoreContext context, IList<T> entities, BulkConfig bulkConfig = null, Action<decimal> progress = null) where T : class
    {
        context.SetAuditInfo<T>(entities);
        return ((DbContext)context).BulkInsertAsync(entities, bulkConfig, progress);
    }

    public static Task BulkUpdateAsync<T>(this CoreContext context, IList<T> entities, IList<T> previousEntities, BulkConfig bulkConfig = null, Action<decimal> progress = null) where T : class
    {
        context.SetAuditInfo<T>(entities, previousEntities);
        return ((DbContext)context).BulkUpdateAsync(entities, bulkConfig, progress);
    }
}

SaveChanges that is called when regular EF is used, has it's own logic for setting AuditInfo.
For example on Update SaveChanges is using ChangeTracker modified Entries and Metadata Property.OriginalValue, that's something Bulk methods don't have because there changes are not tracked, so you have to do it kinda manually if you want all the changes.
But for just setting TimeStamp and CreatedBy, logic is the same as would be in SaveChanges(), so you could have a shared method for this.

@borisdj borisdj changed the title Create extension mehtod Create extension method Jun 26, 2018
@Inlustris
Copy link
Author

Hi Borisdj

Thank you for your information. With those statics i can set the properties i want to set before the items get saved to the database.

The change tracking in EF is indeed a problem that's why i started using EFCore.BulkExtensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants