简体   繁体   中英

ASP.NET Core 2.1 Referring to ApplicationDbContext from Class Library

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)'

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 DbContext is fine, but you have to register it with dependency injection and inject it into your classes instead of using new.

Your startup.cs ConfigureServices method should have your database with the connection string.

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

Then, on the classes you want to use them (like a Controller ) you inject it into the constuctor.

public class HomeController : Controller
{
    private readonly ApplicationDbContext _db;

    public HomeController(ApplicationDbContext db)
    {
        _db = db;
    }
}

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