简体   繁体   English

EF Core IdentityDbContext中的SaveChangesAsync

[英]SaveChangesAsync in EF Core IdentityDbContext

I'm trying to use Entity Framework Core Identity in an ASP.NET Core application 我正在尝试在ASP.NET Core应用程序中使用Entity Framework Core Identity

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中的错误,指出

'AppDbContext' does not implement interface member 'IAppDbContext.SaveChangesAsync()' 'AppDbContext'未实现接口成员'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 . 如果AppDbContext从继承DbContext insted的的IdentityDbContext错误不会出现,但使用的身份应该被继承IdentityDbContext

How can I get around this? 我该如何解决?

It's weird, because this error should shown in both cases. 很奇怪,因为在两种情况下都应显示此错误。 Anyway, DbContext doesn't have method: 无论如何, DbContext没有方法:

Task<int> SaveChangesAsync()

It has method: 它具有方法:

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

To get around this case, you should wrap DbContext.SaveChangesAsync method: 要避免这种情况,您应该包装DbContext.SaveChangesAsync方法:

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

    public Task<int> SaveChangesAsync() => base.SaveChangesAsync();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET 5 EF Core SaveChangesAsync 因错误而挂起 - .NET 5 EF Core SaveChangesAsync hangs on errors .Net Core控制台应用程序-EF Core SaveChangesAsync()不提交对数据库的更新 - .Net Core Console App - EF Core SaveChangesAsync() not committing updates to database 是否需要在.NET Core 2.0和EF下使用IdentityDbContext进行JWT身份验证? - Is it required to use IdentityDbContext for JWT authentication under .NET Core 2.0 and EF? EF Core 不会在 SaveChanges()/SaveChangesAsync() 上引发异常 - EF Core doesn't throw exceptions on SaveChanges()/SaveChangesAsync() .net Core 2.2 EF:SaveChangesAsync() 将不相关的列设置为空 - .net Core 2.2 EF: SaveChangesAsync() setting unrelated columns to null EF Core 并在调用 SaveChangesAsync() 之前引用实体的 ID - EF Core and referencing the ID of a entity before calling SaveChangesAsync() 在.BeginTransaction() EF Core 3.1 中调用.SaveChangesAsync() 多少次 - How many times to call .SaveChangesAsync() in .BeginTransaction() EF Core 3.1 EF SaveChangesAsync非常慢 - EF SaveChangesAsync extremely slow EF 中的事务性 SaveChangesAsync - Transactional SaveChangesAsync in EF 应用程序长时间闲置后,EF Core AddAsync和SaveChangesAsync稍慢 - EF Core AddAsync and SaveChangesAsync slightly slower after application is idle for a long time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM