2

I've faced the problem with using ExecuteDelete in the abstract generic class. Can someone explain in what cases ExecuteDelete and ExecuteUpdate cannot be used? Tagret framework is set to .NET 7

Here is how my code looks:

using Microsoft.EntityFrameworkCore;

namespace ProjectX.Common.Repositories;

public abstract class BaseRepository<T, V> : IRepository<T, V>
where T : Entity
where V : struct
{
protected readonly DbContext _context;

public BaseRepository(DbContext context)
{
    _context = context;
}

public virtual async Task<bool> DeleteAsync(T entity, CancellationToken cancellationToken = default)
{
    var deleted = await _context.Set<T>()
                 .Where(t => t.Id.Equals(entity.Id))
                 .ExecuteDeleteAsync(); // Here is the problem. This method cannot be found. Target Framework is .Net 7 and EF Core version is 7.0.7

    return deleted > 0;
}
}

EF Core version: 7.0.7 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: NET 7.0 IDE: Visual Studio 2022 latest version

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
KyryloDev
  • 31
  • 4
  • Can you provide a link to the `ExecuteDeleteAsync` method you are *trying* to call? (It looks like you're trying to call it on an `IQueryable`... was that your intention?) – Jon Skeet Jul 26 '23 at 13:44
  • @JonSkeet, [ExecuteUpdate and ExecuteDelete](https://learn.microsoft.com/en-us/ef/core/saving/execute-insert-update-delete) - they are created for `IQueryable`. – Svyatoslav Danyliv Jul 26 '23 at 13:56
  • Yes, That's right! I am trying to call it on IQueryable, but it looks like this method cannot be found. This is what I am trying to do: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#executeupdate-and-executedelete-bulk-updates – KyryloDev Jul 26 '23 at 13:57
  • At least you forgot `await`. – Svyatoslav Danyliv Jul 26 '23 at 13:57
  • @SvyatoslavDanyliv , no, it is not about await, I added it anyway, thanks for noticing. Method cannot just be used tor something. Visual Studio just do not see it in this class, but in my other classes which inherit from BaseRepository these methods work fine. – KyryloDev Jul 26 '23 at 14:05
  • 4
    Ensure that your project references `Microsoft.EntityFrameworkCore.Relational` – Svyatoslav Danyliv Jul 26 '23 at 14:09
  • 1
    Yup, a missing package sounds like the issue (in terms of compilation). – Jon Skeet Jul 26 '23 at 14:13
  • @SvyatoslavDanyliv Yes, you are right! I completely forgot about this package because package references were configured by my college. Thank you very much! – KyryloDev Jul 26 '23 at 14:41

2 Answers2

1

It was missing package reference to Microsoft.EntityFrameworkCore.Relational

KyryloDev
  • 31
  • 4
-1

Please see: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-7.0/whatsnew#inheritance-and-multiple-tables

"Adding a filter to the query often means the operation will fail with both the TPC and TPT strategies. This is again because the rows may need to be deleted from multiple tables. For example, this query:

await context.Posts.Where(p => p.Author!.Name.StartsWith("Arthur")).ExecuteDeleteAsync();

fails when using TPC or TPT."

Issue #10879 tracks adding support for automatically sending multiple commands in these scenarios. Vote for this issue if it's something you would like to see implemented.

tl;dr
ExecuteUpdate/Delete can only act on a single table and generic entity may have relations to other tables when working with TPC/TPT mapping strategies.

KrystianL
  • 1
  • 2
  • This demonstrates issues when actually using the methods, but doesn't address the question, which is a compilation error due to the method not being found. – Jon Skeet Jul 26 '23 at 14:14