简体   繁体   English

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

[英]Asp.net core create interface for identity

I am creating an asp.net core web application and I am trying to create an interface for my db context in order to use it in my bussiness logic layer.我正在创建一个 asp.net 核心 web 应用程序,我正在尝试为我的数据库上下文创建一个接口,以便在我的业务逻辑层中使用它。 The interface is made as followed:界面制作如下:

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

The Implementation of the Interface is as followed:接口的实现如下:

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

The problem is that when i Inject the interface and I try to use _context.Users it shows the error that:问题是,当我注入接口并尝试使用_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?).

I understand that on the implementation, _context.Users comes from the parent class IdentityDbContext , but how can I also add it to the interface so I can use it?我知道在实现上, _context.Users来自父 class IdentityDbContext ,但是我怎样才能将它添加到界面中以便我可以使用它? Thank you!谢谢!

You can add Users to your interface.您可以将用户添加到您的界面。 Try to update it following way:尝试通过以下方式更新它:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM