SaveChangesAsync in EF Core IdentityDbContext

.net-core asp.net-identity c# entity-framework-core

Question

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?

1
3
11/30/2018 5:07:32 AM

Accepted Answer

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();
}
5
11/30/2018 6:02:51 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow