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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With