Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with using ExecuteDelete and ExecuteUpdate in .NET 7

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

like image 537
KyryloDev Avatar asked Mar 10 '26 17:03

KyryloDev


1 Answers

It was missing package reference to Microsoft.EntityFrameworkCore.Relational

like image 169
KyryloDev Avatar answered Mar 12 '26 05:03

KyryloDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!