Asp.net 核心创建身份接口

Asp.net core create interface for identity

我正在创建一个 asp.net 核心 Web 应用程序,我正在尝试为我的数据库上下文创建一个接口,以便在我的业务逻辑层中使用它。界面制作如下:

public interface IAppDbContext
{
    public DbSet<Country> Countries { get; set; }
    public DbSet<City> Cities { get; set; }
    Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}

接口实现如下:

public class AppDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string> , IAppDbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
            : base(options)
        {

        }

        public DbSet<Country> Countries { get; set; }
        public DbSet<City> Cities { get; set; }
    }

问题是,当我注入接口并尝试使用 _context.Users 时,它显示的错误是:

'IAppDbContext' does not contain a definition for 'Users' and no accessible extension method 'Users' accepting a first argument of type 'IAppDbContext' could be found (are you missing a using directive or an assembly reference?).

我知道在实现中,_context.Users 来自父级 class IdentityDbContext,但我如何才能将它也添加到界面以便我可以使用它?谢谢!

您可以将用户添加到您的界面。尝试按照以下方式更新它:

public interface IAppDbContext
{
    public DbSet<Country> Countries { get; set; }
    public DbSet<City> Cities { get; set; }
    DbSet<ApplicationUser> Users { get; set; }
    Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}