EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

Batch delete does not work for keyless tables

Open Danielku15 opened this issue 3 years ago • 3 comments

Description

This request is similar to #557. For keyless entities it is not possible to do a DeleteFromQuery. Once you have a keyless entity with .HasNoKey() and you attempt to delete it via query you will run into an exception.

await context.KeyLess.Where(k => k.CreationTime < threshold).DeleteFromQueryAsync();

I assume the default generation of SQL is something like

DELETE FROM {table} WHERE PK in (SELECT PK FROM <query>)

and therefore a PK is needed. Makes sense, but for simple queries like above not really a PK is needed and it would be great if it can be supported. I would be fine with having a await context.KeyLess.DeleteByConditionAsync(k=>k.CreationTime < threshold) to avoid potential unexpected side effects.

Exception

Exception message: Oops! No key has been found. A keyless entity is not supported.

Stack trace:
   at Z.EntityFramework.Extensions.BatchDelete.Execute[T](IQueryable`1 query)
   at BatchDeleteExtensions.DeleteFromQuery[T](IQueryable`1 query, Action`1 batchDeleteBuilder)
   at BatchDeleteExtensions.?`1.?()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---

Fiddle or Project

Unfortunately DotNetFiddle is blocked by our IT but here a gist reproducing the issue. https://gist.github.com/Danielku15/8c3ddc0edd0dd39046650544e9e3a9ce

Further technical details

  • EF version: 3.1.6
  • EF Plus version: 3.0.63
  • Database Provider: SQL Server

Danielku15 avatar Oct 06 '20 08:10 Danielku15

Hello @Danielku15 ,

Unfortunately, nothing will be done in the short term.

However, when we will re-write that feature to allows the query directly to be executed without requiring the primary key (so the condition directly is done on the table), we will for sure look at it.

Best Regards,

Jon


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

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

JonathanMagnan avatar Oct 06 '20 16:10 JonathanMagnan

Hmm... Have the same issue right now with UpdateAsync(). Is there a solution right now?

kevinvenclovas avatar Sep 20 '21 12:09 kevinvenclovas

Hello @kevinvenclovas ,

Unfortunately, there is no solution right now.

We hope one day to get enough time to do a request like this one but time is always missing ;(

JonathanMagnan avatar Sep 21 '21 23:09 JonathanMagnan