簡體   English   中英

EF Core IdentityDbContext中的SaveChangesAsync

[英]SaveChangesAsync in EF Core IdentityDbContext

我正在嘗試在ASP.NET Core應用程序中使用Entity Framework Core Identity

我創建了數據庫上下文及其接口,如下所示:

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

問題出在這里,它顯示AppDbContext中的錯誤,指出

'AppDbContext'未實現接口成員'IAppDbContext.SaveChangesAsync()'

如果AppDbContext從繼承DbContext insted的的IdentityDbContext錯誤不會出現,但使用的身份應該被繼承IdentityDbContext

我該如何解決?

很奇怪,因為在兩種情況下都應顯示此錯誤。 無論如何, DbContext沒有方法:

Task<int> SaveChangesAsync()

它具有方法:

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

要避免這種情況,您應該包裝DbContext.SaveChangesAsync方法:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM