简体   繁体   中英

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?

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM