![](/img/trans.png)
[英]How to implement database resiliency while having a scoped Dbcontext in EF Core?
[英]Lifetime of scoped EF Core DbContext in WPF
我想在 .NET Core 3.1 WPF 應用程序中使用 EF Core 和 .NET Core 依賴注入。
在 ASP.NET 中,作用域 DbContext 的生命周期將是一個 HTTP 請求,但在 WPF 中,它將是推薦的整個應用程序的生命周期,這不是。
那么如何在 WPF .NET 核心應用程序中管理 DbContext 的生命周期?
我建議這樣定義:
services.AddDbContext<AppDbContext>(options => options
.UseSqlServer(configuration.GetConnectionString("LocalDB")), ServiceLifetime.Transient, ServiceLifetime.Transient);
這樣每個 Model 都有自己的 DbContext。
另一種解決方案是聲明 Singleton DbContextFactory 並調用其Create
方法以每次使用創建新的 DbContext。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.