Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SaveChangesAsync in EF Core IdentityDbContext

I'm trying to use Entity Framework Core Identity in an ASP.NET Core application

I have created Database Context and its interface as follows:

public class AppDbContext : IdentityDbContext<AppUser>, IAppDbContext
{
    public AppDbContext (DbContextOptions<AppDbContext> options) : base(options)
    { }

    public DbSet<AppUser> AppUser { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

public interface IAppDbContext
{
    DbSet<AppUser> AppUser { get; set; }

    int SaveChanges();
    Task<int> SaveChangesAsync();
}

Here the issue is, it shows an error in AppDbContext, stating

'AppDbContext' does not implement interface member 'IAppDbContext.SaveChangesAsync()'

If the AppDbContext is inherited from the DbContext insted of IdentityDbContext error would not appear, but to use Identity it should be inherited from IdentityDbContext.

How can I get around this?

like image 321
Nalaka526 Avatar asked Mar 05 '23 23:03

Nalaka526


1 Answers

It's weird, because this error should shown in both cases. Anyway, DbContext doesn't have method:

Task<int> SaveChangesAsync()

It has method:

Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))

To get around this case, you should wrap DbContext.SaveChangesAsync method:

public class AppDbContext : IdentityDbContext<AppUser>, IAppDbContext
{
    ...

    public Task<int> SaveChangesAsync() => base.SaveChangesAsync();
}
like image 89
Vlad Avatar answered Mar 08 '23 01:03

Vlad