zzzprojects / EntityFramework-Plus

Entity Framework Plus extends your DbContext with must-haves features: Include Filter, Auditing, Caching, Query Future, Batch Delete, Batch Update, and more
https://entityframework-plus.net/
MIT License
2.25k stars 318 forks source link

Batch Delete with HasQueryFilter #417

Open pgrm opened 5 years ago

pgrm commented 5 years ago

Hello, we switched from EntityFramework-Plus filter extension to EF Core HasQueryFilter because we started using views, but this again doesn't seem to work with batch delete:

System.Collections.Generic.KeyNotFoundException: The given key '__ef_filter__PROPERTYNAME_0' was not present in the dictionary.

any plans to support the ef-core query filter together with your batch / bulk operations?

Thank you!

JonathanMagnan commented 5 years ago

Hello @pgrm ,

Do you think you could provide a project sample with this issue?

It will help my developer investigate the issue more efficiently.

We now always ask for a project sample even if the issue is easy to reproduce. As a free product, we must find some way to save time to offer an overall better experience for everyone and release fixes faster.

You can send it to: info@zzzprojects.com if you need to keep the source private

Best Regards,

Jonathan


Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsEntity Framework ClassicBulk OperationsDapper PlusLinqToSql Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function

pgrm commented 5 years ago

Hello @JonathanMagnan

I totally understand that, unfortunately, currently I don't havea lot of time on my hands to set up a test project. Do you maybe have some sample projects with .net core, where you use those bulk operations? Or even better, some integration tests?

I should be able to modify them with only2 lines of code and you'd already know what exactly causes the issue for me without needing to analyze if I set up everything correctly.

JonathanMagnan commented 5 years ago

We just need a simple standalone project that contains the issue that we can easily use to reproduce and test it.

Best Regards,

Jonathan

pgrm commented 5 years ago

@JonathanMagnan so I've copied one of your online examples (https://dotnetfiddle.net/R6D5BX) and adjusted it to work with ef core and sqlite so I can show the error. This is the extra line which break everything: https://gist.github.com/pgrm/6e7316a776fbb270b2dbd62caa5fe017#file-brokenbatchdeleteexample-cs-L69 when this line is active I get the error:

Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key '__ef_filter__ef_filter_0' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Z.EntityFramework.Plus.BatchDelete.CreateCommand(IQueryable query, IEntityType entity) in C:\Users\Jonathan\Desktop\Z\TFS\EFPlus\Z.EntityFramework.Plus\shared\Z.EF.Plus.BatchDelete.Shared\BatchDelete.cs:line 770

   at Z.EntityFramework.Plus.BatchDelete.Execute[T](IQueryable`1 query) in C:\Users\Jonathan\Desktop\Z\TFS\EFPlus\Z.EntityFramework.Plus\shared\Z.EF.Plus.BatchDelete.Shared\BatchDelete.cs:line 344
   at BrokenBatchDeleteExample.Program.Main() in MY_PATH\BrokenBatchDeleteExample\BrokenBatchDeleteExample\Program.cs:line 20

When I comment it out, it works, however, HasQueryFilter works correctly with other where conditions from EF Core, so I guess there is some parameter missing when the expression is being compiled for the batch delete functionality?

Hope this helps

JonathanMagnan commented 5 years ago

Thank you,

The Gist is what we were looking for (https://gist.github.com/pgrm/6e7316a776fbb270b2dbd62caa5fe017)

I will assign a developer to it.

Best Regards,

Jonathan

JonathanMagnan commented 5 years ago

Hello @pgrm ,

We investigated this issue. We have no found how to generate parameter from the QueryFilter.

Meanwhile, a temporary solution on your side is perhaps maybe only to ignore those filters for the current query with IgnoreQueryFilters See: https://docs.microsoft.com/en-us/ef/core/querying/filters#disabling-filters

We will continue to investigate it to find a way to get those parameter values.

Best Regards,

Jonathan

JonathanMagnan commented 5 years ago

Btw, a new version will be soon available with a better error message to let people know about using IgnoreQueryFilters.

pgrm commented 5 years ago

@JonathanMagnan thx for the update

ismcagdas commented 5 years ago

Hi @JonathanMagnan

First of all, thank you for this great library. I have tried BatchUpdate and faced a similar exception.

The exception occurs here https://github.com/zzzprojects/EntityFramework-Plus/blob/master/src/shared/Z.EF.Plus.BatchUpdate.Shared/BatchUpdate.cs#L894 because queryContext.ParameterValues is empty.

I think the ParameterValues are not correctly populated here https://github.com/zzzprojects/EntityFramework-Plus/blob/master/src/shared/Z.EF.Plus._Core.Shared/EFCore/IQueryable%60/CreateCommand.cs#L38. Somehow queryContext.ParameterValues are not filled with the values of query filter parameters.

JonathanMagnan commented 5 years ago

Hello @ismcagdas ,

Yes, that's exactly the issue.

That will happen with all our features that require to generate the command on our side.

Perhaps EF Core 2.x has now better method to get the query from (this one was created in early stage of EF Core 1.x)

ismcagdas commented 5 years ago

@JonathanMagnan did you have a chance to take a look into this issue ? I would love to help if I can.

JonathanMagnan commented 5 years ago

Hello @ismcagdas ,

Yes, we got the chance at multiple time but we never find out a way to solve it ;(

So for now, the only solution on your side is to apply IgnoreQueryFilters. We don't wanna apply it on our side by default since it will mislead.

Best Regards,

Jonathan

ismcagdas commented 5 years ago

Thanks a lot.

se commented 5 years ago

Hi guys, Is there any update about this issue?

JonathanMagnan commented 5 years ago

Hello @se ,

We didn't investigate it again. So that's the same status

Yes, we got the chance at multiple time but we never find out a way to solve it ;(

So for now, the only solution on your side is to apply IgnoreQueryFilters. We don't wanna apply it on our side by default since it will mislead.

We will probably try again when the version 3.0 will be out.

alirezanet commented 5 years ago

The given key '__ef_filter__ef_filter_0' was not present in the dictionary.

I have same problem and its not possible for us to Ignore query filters ... is there any hope to fix this issue soon ?