简体   繁体   中英

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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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