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

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

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

问题

以下是翻译好的内容:

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

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

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

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

  1. var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw an InvalidOperationException("Connection string 'DefaultConnection' not found.");
  2. void BuildOptions(DbContextOptionsBuilder options) => options.UseSqlServer(connectionString).EnableSensitiveDataLogging(); // todo: not for production
  3. builder.Services.AddDbContext<OpmContext>(BuildOptions);
  4. builder.Services.AddDbContextFactory<OpmContext>(BuildOptions);
  5. builder.Services.AddDatabaseDeveloperPageExceptionFilter();
  6. builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
  7. .AddEntityFrameworkStores<OpmContext>();

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

  1. public class OpmContext : IdentityDbContext
  2. {
  3. public OpmContext(DbContextOptions<OpmContext> options) : base(options)
  4. {
  5. }
  6. }

我得到以下错误信息:

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:

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

When I change that to use

  1. var connectionString = builder.Configuration.GetConnectionString(&quot;DefaultConnection&quot;) ?? throw new InvalidOperationException(&quot;Connection string &#39;DefaultConnection&#39; not found.&quot;);
  2. void BuildOptions(DbContextOptionsBuilder options) =&gt; options.UseSqlServer(connectionString).EnableSensitiveDataLogging(); // todo: not for production
  3. builder.Services.AddDbContext&lt;OpmContext&gt;(BuildOptions);
  4. builder.Services.AddDbContextFactory&lt;OpmContext&gt;(BuildOptions);
  5. builder.Services.AddDatabaseDeveloperPageExceptionFilter();
  6. builder.Services.AddDefaultIdentity&lt;IdentityUser&gt;(options =&gt; options.SignIn.RequireConfirmedAccount = true)
  7. .AddEntityFrameworkStores&lt;OpmContext&gt;();

and change my own DbContext to inherit from IdentityContext:

  1. public class OpmContext : IdentityDbContext
  2. {
  3. public OpmContext(DbContextOptions&lt;OpmContext&gt; options) : base(options)
  4. {
  5. }
  6. }

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

更改顺序:

  1. builder.Services.AddDbContext&lt;OpmContext&gt;(options =&gt;
  2. options.UseSqlServer(connectionString));
  3. builder.Services
  4. .AddDbContextFactory&lt;OpmContext&gt;(options =&gt;
  5. options.UseSqlServer(connectionString));
  6. builder.Services.AddDatabaseDeveloperPageExceptionFilter();
  7. builder.Services.AddDefaultIdentity&lt;IdentityUser&gt;(options =&gt; options.SignIn.RequireConfirmedAccount = true)
  8. .AddEntityFrameworkStores&lt;OpmContext&gt;();
英文:

Change the order:

  1. builder.Services.AddDbContextFactory&lt;OpmContext&gt;(options =&gt;
  2. options.UseSqlServer(connectionString));
  3. builder.Services
  4. .AddDbContext&lt;OpmContext&gt;(options =&gt;
  5. options.UseSqlServer(connectionString));
  6. builder.Services.AddDatabaseDeveloperPageExceptionFilter();
  7. builder.Services.AddDefaultIdentity&lt;IdentityUser&gt;(options =&gt; options.SignIn.RequireConfirmedAccount = true)
  8. .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:

确定