扩展基架 DbContext 以使用 IdentityDbContext

Extend Scaffolded DbContext to use IdentityDbContext

提问人:Alisbha Khan 提问时间:11/13/2023 最后编辑:Qiang FuAlisbha Khan 更新时间:11/13/2023 访问量:51

问:

我正在使用 .NET8 RC2 版本以及 EF Core 和我使用 dotnet ef dbcontext 基架命令为现有数据库搭建了基架。

此命令生成了 Entities 和 AutoGeneratedDbContext

我知道我可以通过创建一个继承此 DbContext 的类来扩展其功能,例如

public class CustomDbContext : AutoGeneratedDbContext
{
 public CustomDbContext()
 {
 }
 public CustomDbContext(DbContextOptions<AutoGeneratedDbContext> options) : base(options)
 {
 }
}

这工作正常,但现在我想使用身份验证,为此我必须添加 IdentityDbContext

现在,如果我像这样创建 IdentityDbContext

public class IdentityDbContext(DbContextOptions<IdentityDbContext> options) : IdentityDbContext<AppUser>(options)
{
}

那么我将不得不再注册一个 DbContext,我认为这不是我应该做的。如果我不注册 IdentityDbContext,那么以下将不起作用。

builder.Services.AddIdentityCore<AppUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<IdentityDbContext>()
    .AddSignInManager()
    .AddDefaultTokenProviders();

我知道我不能用我的 CustomDbContext 进行多重继承,这就是为什么我有点困惑并且不知道下一步该怎么做才能使其成为具有 Identity 的单个 DbContext

实体框架 asp.net-core asp.net-identity net-8.0

评论

0赞 Qiang Fu 11/21/2023
我认为可以从 IdentityDbContext 继承你的。AutoGeneratedDbContext

答:

0赞 Qiang Fu 11/27/2023 #1

您可以直接重用 来扩展更多功能。但是这里有一个技巧,您可以尝试以下操作:AutoGeneratedDbContext

  1. 我有一个数据库“test02”包含一个“Students”表
    enter image description here
  2. 使用数据库命令生成上下文 “Test02Context” 3.In “Test02Context”,删除 “OnModelCreating” “OnModelCreatingPartial”
    enter image description here
    方法。如下图所示:
    Scaffold-DbContext "server=192.168.2.68;database=test02;user=mysql1;password=xxxxx" Pomelo.EntityFrameworkCore.MySql -OutputDir Models
public partial class Test02Context : DbContext
{
    public Test02Context()
    {
    }

    public Test02Context(DbContextOptions<Test02Context> options)
        : base(options)
    {
    }

    public virtual DbSet<Student> Students { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
        => optionsBuilder.UseMySql("server=192.168.2.68;database=test02;user=mysql1;password=xxxxx", Microsoft.EntityFrameworkCore.ServerVersion.Parse("8.0.32-mysql"));
}

4.使用 code-fisrt 命令生成第一个迁移文件“initial”。Add-migration initialenter image description here

5.In“Test02Context”中,将 替换为 .并在程序.cs中。直接使用此自动生成上下文。DbContextIdentityDbContext

 Test02Context : IdentityDbContext
builder.Services.AddIdentityCore<IdentityUser>()
    .AddEntityFrameworkStores<Test02Context>()
    .AddSignInManager()
    .AddDefaultTokenProviders();

6.现在生成第二次迁移成功后,删除第一个迁移文件。然后,我们将直接从第二次迁移进行迁移。enter image description here
7.检查数据库,更新身份表。
Add-migration secondUpdate-database
enter image description here