Asp.net核心为身份创建接口

时间:2019-10-24 18:21:31

标签: c# asp.net-core asp.net-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来自父类IdentityDbContext,但是如何将其添加到接口中以便使用呢?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将“用户”添加到您的界面。尝试通过以下方式更新它:

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