ASP.NET Core 2.1从类库引用ApplicationDbContext

时间:2018-09-28 14:57:25

标签: c# asp.net asp.net-mvc asp.net-core

我有这两个项目App.ClassLibrary和App.Data

我按照本教程https://garywoodfine.com/using-ef-core-in-a-separate-class-library-project/的要求将迁移和模型移至App.ClassLibrary。这些步骤之一是在类库中创建一个ApplicationDbContext:

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

在App.Data中,我正在尝试引用ApplicationDbContext,但是到目前为止,这是行不通的

private ApplicationDbContext dbContext = new ApplicationDbContext();

我以前在ASP.NET MVC 5中有一个类似的项目,具有这样的ApplicationDBContext:

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

}

我可以使用

private ApplicationDbContext dbContext = new ApplicationDbContext();

没有任何问题,但是现在我猜测是因为版本和框架不同而无法正常工作。不知道该怎么做才能获得dbcontext。

编辑:

这是我在.Net Core 2.1应用中遇到的错误

没有提供与'ApplicationDbContext.ApplicationDbContext(DbContextOptions)'所需的形式参数'options'相对应的参数

2 个答案:

答案 0 :(得分:0)

您的ApplicationDbContext应该是:

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

    }
}

您应该像这样注册它,具体取决于您使用的SqlServer数据库服务器:

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

答案 1 :(得分:0)

您的DbContext很好,但是您必须通过依赖项注入对其进行注册,然后将其注入类中,而不是使用new。

您的startup.cs ConfigureServices方法应使您的数据库具有连接字符串。

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

然后,在要使用它们的类上(例如Controller),将其注入到构造器中。

public class HomeController : Controller
{
    private readonly ApplicationDbContext _db;

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