I have these 2 projects App.ClassLibrary and App.Data

I followed this tutorial https://garywoodfine.com/using-ef-core-in-a-separate-class-library-project/ to move my migrations and models to App.ClassLibrary. One of those steps was to create an ApplicationDbContext in the class library:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
    public DbSet<Users> Users { get; set; }
    public DbSet<UserRoles> UserRoles { get; set; }
    public DbSet<Applications> Applications { get; set; }
    public DbSet<Roles> Roles { get; set; }
    public DbSet<EventLogs> EventLogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

        modelBuilder.HasDefaultSchema(schema: DatabaseGlobals.SchemaName);
        modelBuilder.Entity<Users>();
        modelBuilder.Entity<UserRoles>();
        modelBuilder.Entity<Applications>();
        modelBuilder.Entity<Roles>();
        modelBuilder.Entity<EventLogs>();
        base.OnModelCreating(modelBuilder);
    }

    public override int SaveChanges()
    {
        Audit();
        return base.SaveChanges();
    }

    public async Task<int> SaveChangesAsync()
    {
        Audit();
        return await base.SaveChangesAsync();
    }

    private void Audit()
    {
        var entries = ChangeTracker.Entries().Where(x => x.Entity is Users && (x.State == EntityState.Added || x.State == EntityState.Modified));
        foreach (var entry in entries)
        {
            if (entry.State == EntityState.Added)
            {
                ((Users)entry.Entity).CreatedOn = DateTime.UtcNow;
            }
        ((Users)entry.Entity).UpdatedOn = DateTime.UtcNow;
        }
    }
}

In App.Data, I am trying to reference ApplicationDbContext but so far this isn't working

private ApplicationDbContext dbContext = new ApplicationDbContext();

I used to have a similar project in ASP.NET MVC 5 with an ApplicationDBContext like this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public virtual DbSet<AspNetUsersExtendedDetails> AspNetUsersExtendedDetails { get; set; }
    public virtual DbSet<AspNetApplications> AspNetApplications { get; set; }
    public virtual DbSet<AspNetEventLogs> AspNetEventLogs { get; set; }
    public virtual DbSet<AspNetRolesExtendedDetails> AspNetRolesExtendedDetails { get; set; }
    public virtual DbSet<AspNetUserRolesExtendedDetails> AspNetUserRolesExtendedDetails { get; set; }
    public virtual DbSet<AspNetUserAccessTokens> AspNetUserAccessTokens { get; set; }
    public ApplicationDbContext() : base("ManagementStudio")
    {

    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

}

I could use

private ApplicationDbContext dbContext = new ApplicationDbContext();

without any issues but now I'm guessing because of the different versions and framework its not working. Not sure what I can do to get the dbcontext.

EDIT:

This is the error I get in my .Net Core 2.1 App

There is no argument given that corresponds to the required formal parameter 'options' of 'ApplicationDbContext.ApplicationDbContext(DbContextOptions)'

  • Why isn't it working? Do you get an error? What error? BTW ASP.NET Core uses dependency injection extensively and contexts are injected. You shouldn't have private fields like that. ASP.NET Core knows about injected contexts and closes them when a request ends. It doesn't know about your dbContext field – Panagiotis Kanavos 20 mins ago
  • There is no argument given that corresponds to the required formal parameter 'options' of 'ApplicationDbContext.ApplicationDbContext(DbContextOptions<ApplicationDbContext>)' This is the error – JianYA 20 mins ago
  • I'm not sure what parameters I should insert in my app.data declaration – JianYA 19 mins ago
  • An options parameter. That's why you shouldn't try to create repositories like this. Check Configuring a DbContext. It shows how you configure context options in the service configuration section. ASP.NET Core will use that information to construct the context and inject it into your controller – Panagiotis Kanavos 17 mins ago
  • Sorry I'm confused. I'm looking through the guide now. What would my blog.db be in this case? – JianYA 10 mins ago

Your ApplicationDbContext should be:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public virtual DbSet<AspNetUsersExtendedDetails> AspNetUsersExtendedDetails { get; set; }
    public virtual DbSet<AspNetApplications> AspNetApplications { get; set; }
    public virtual DbSet<AspNetEventLogs> AspNetEventLogs { get; set; }
    public virtual DbSet<AspNetRolesExtendedDetails> AspNetRolesExtendedDetails { get; set; }
    public virtual DbSet<AspNetUserRolesExtendedDetails> AspNetUserRolesExtendedDetails { get; set; }
    public virtual DbSet<AspNetUserAccessTokens> AspNetUserAccessTokens { get; set; }
    public ApplicationDbContext(DbContextOptions options) : base(options)
    {

    }
}

And your should register it like that, depending on the DB server you use, for SqlServer:

services.AddDbContext<ApplicationDbContext>(options =>  
    options.UseSqlServer(Configuration.GetConnectionString("DbContext"))

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.