英文:
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("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
When I change that to use
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new 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>();
and change my own DbContext to inherit from IdentityContext:
public class OpmContext : IdentityDbContext
{
public OpmContext(DbContextOptions<OpmContext> 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<OpmContext> DbFactory
in razor pages or own services.
答案1
得分: 2
更改顺序:
builder.Services.AddDbContext<OpmContext>(options =>
options.UseSqlServer(connectionString));
builder.Services
.AddDbContextFactory<OpmContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<OpmContext>();
英文:
Change the order:
builder.Services.AddDbContextFactory<OpmContext>(options =>
options.UseSqlServer(connectionString));
builder.Services
.AddDbContext<OpmContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<OpmContext>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论