Blazor Server + EF Core 7 + Identity –> 使用自定义的 DbContext 和 DbContextFactory

huangapple go评论53阅读模式
英文:

Blazor Server + EF Core 7 + Idenitty --> using a custom DbContext and DbContextFactory

问题

以下是翻译好的内容:

问题很简单:如何使用Identity创建自己的上下文,而不使用默认的ApplicationDbContext。

在Program.cs中,默认情况下你有:

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));

当我更改为使用以下代码:

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw an InvalidOperationException("Connection string 'DefaultConnection' not found.");
void BuildOptions(DbContextOptionsBuilder options) => options.UseSqlServer(connectionString).EnableSensitiveDataLogging(); // todo: not for production

builder.Services.AddDbContext<OpmContext>(BuildOptions);
builder.Services.AddDbContextFactory<OpmContext>(BuildOptions);

builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<OpmContext>();

并将我的自定义DbContext更改为继承自IdentityContext:

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

我得到以下错误信息:

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.IDbContextFactory`1[Foo.Bar.Data.OpmContext] Lifetime: Singleton

我查看了https://stackoverflow.com/questions/64169025/using-identity-with-adddbcontextfactory-in-blazor,但似乎无法使其工作。有什么建议吗?

目标是在Blazor页面或自己的服务中注入并使用IDbContextFactory<OpmContext> DbFactory

英文:

The question is simple: how can I make my own context using the identity. I don't want to use the default ApplicationDbContext.

In Program.cs by default you have:

var connectionString = builder.Configuration.GetConnectionString(&quot;DefaultConnection&quot;) ?? throw new InvalidOperationException(&quot;Connection string &#39;DefaultConnection&#39; not found.&quot;);
builder.Services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
    options.UseSqlServer(connectionString));

When I change that to use

var connectionString = builder.Configuration.GetConnectionString(&quot;DefaultConnection&quot;) ?? throw new InvalidOperationException(&quot;Connection string &#39;DefaultConnection&#39; not found.&quot;);
void BuildOptions(DbContextOptionsBuilder options) =&gt; options.UseSqlServer(connectionString).EnableSensitiveDataLogging(); // todo: not for production

builder.Services.AddDbContext&lt;OpmContext&gt;(BuildOptions);
builder.Services.AddDbContextFactory&lt;OpmContext&gt;(BuildOptions);

builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity&lt;IdentityUser&gt;(options =&gt; options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores&lt;OpmContext&gt;();

and change my own DbContext to inherit from IdentityContext:

public class OpmContext : IdentityDbContext 
{
    public OpmContext(DbContextOptions&lt;OpmContext&gt; options) : base(options)
    {
    }
}

I get:

> System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.EntityFrameworkCore.IDbContextFactory`1[Foo.Bar.Data.OpmContext] Lifetime: Singleton

I had a look at https://stackoverflow.com/questions/64169025/using-identity-with-adddbcontextfactory-in-blazor but can't seem to get it working. Any suggestions?

The goal is to inject and use IDbContextFactory&lt;OpmContext&gt; DbFactory in razor pages or own services.

答案1

得分: 2

更改顺序:

builder.Services.AddDbContext&lt;OpmContext&gt;(options =&gt;
    options.UseSqlServer(connectionString));

builder.Services
    .AddDbContextFactory&lt;OpmContext&gt;(options =&gt;
        options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity&lt;IdentityUser&gt;(options =&gt; options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores&lt;OpmContext&gt;();
英文:

Change the order:

builder.Services.AddDbContextFactory&lt;OpmContext&gt;(options =&gt;
    options.UseSqlServer(connectionString));

builder.Services
    .AddDbContext&lt;OpmContext&gt;(options =&gt;
        options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity&lt;IdentityUser&gt;(options =&gt; options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores&lt;OpmContext&gt;();

huangapple
  • 本文由 发表于 2023年2月16日 18:54:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定