【问题标题】:SaveChangesAsync in EF Core IdentityDbContextEF Core IdentityDbContext 中的 SaveChangesAsync
【发布时间】:2019-05-02 06:09:24
【问题描述】:

我正在尝试在 ASP.NET Core 应用程序中使用 Entity Framework Core Identity

我已经创建了Database Context及其接口,如下:

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 不会出现错误,但要使用 Identity 它应该继承自 IdentityDbContext

我怎样才能解决这个问题?

【问题讨论】:

    标签: c# .net-core asp.net-identity entity-framework-core


    【解决方案1】:

    这很奇怪,因为在这两种情况下都应该显示此错误。反正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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 2018-05-23
      相关资源
      最近更新 更多