- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
Entity Framework contexts should be added to the services container using the
Scoped
lifetime. This is taken care of automatically if you use the helper methods as shown above. Repositories that will make use of Entity Framework should use the same lifetime.
我一直认为,我应该为我必须处理的每个工作单元创建一个新的 Context
。这让我想到,如果我有一个 ServiceA
和 ServiceB
,它们在 DbContext
上应用不同的操作,它们应该获得不同的实例DbContext
.
documentation内容如下:
Transient
objects are always different; a new instance is provided to every controller and every service.
Scoped
objects are the same within a request, but different across different request
回到ServiceA
和ServiceB
,在我看来,Transient
更合适。
我研究过,上下文应该只在每个 HttpRequest
中保存一次,但我真的不明白这是如何工作的。
特别是如果我们看一下一项服务:
using (var transaction = dbContext.Database.BeginTransaction())
{
//Create some entity
var someEntity = new SomeEntity();
dbContext.SomeEntity.Add(someEntity);
//Save in order to get the the id of the entity
dbContext.SaveChanges();
//Create related entity
var relatedEntity = new RelatedEntity
{
SomeEntityId = someEntity.Id
};
dbContext.RelatedEntity.Add(relatedEntity)
dbContext.SaveChanges();
transaction.Commit();
}
这里我们需要保存上下文以获取与我们刚刚创建的另一个实体相关的实体的 ID。
同时另一个服务可以更新相同的上下文。据我所知,DbContext
不是线程安全的。
在这种情况下我应该使用 Transient
吗?为什么文档建议我应该使用Scoped
?
我是否遗漏了框架的某些重要部分?
最佳答案
正如其他人已经解释过的,您应该对数据库上下文使用范围依赖,以确保它能被正确重用。对于并发性,请记住您也可以异步查询数据库,因此您可能不需要实际的线程。
如果您确实需要线程,即后台 worker ,那么它们的生命周期可能与请求的生命周期不同。因此,那些线程应该不使用从请求范围检索到的依赖项。当请求结束并且其依赖范围正在关闭时,一次性依赖将被妥善处理。对于其他线程,这意味着它们的依赖项可能最终会被处理掉,尽管它们仍然需要它们:坏主意。
相反,您应该为您创建的每个线程显式打开一个新的依赖范围。您可以通过注入(inject) IServiceScopeFactory
来做到这一点并使用 CreateScope
创建范围.结果对象将包含一个服务提供者,您可以从中检索您的依赖项。由于这是一个单独的作用域,因此将在该作用域的生命周期内重新创建作用域依赖关系(如数据库上下文)。
为了避免进入服务定位器模式,您应该考虑让您的线程执行一个中央服务,该服务汇集了所有必要的依赖项。然后线程可以这样做:
using (var scope = _scopeFactory.CreateScope())
{
var service = scope.ServiceProvider.GetService<BackgroundThreadService>();
service.Run();
}
BackgroundThreadService
及其所有依赖项都可以按照常见的依赖注入(inject)方式接收依赖项。
关于c# - 我应该如何在 MVC Core 中管理 DbContext 生命周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46412734/
我已经开始研究使用 Identity 框架的现有 ASP.NET Core 项目。该应用程序使用单个数据库。出于某种原因,应用程序使用两个独立的数据库上下文 - 一个派生自 IdentityDbCon
我刚刚下载了EntityFramework.dll v4.3。我发现了许多比较 DbContext 与 ObjectContext 的问题。但其中大部分是 2010 年或 2011 年初的。 我想阅读
我想做什么 public void SomeTestMethod(){ var person = this.PersonManager.Get(new Guid("someguid"));
在我的应用程序中,我刚刚将 EntityFramework 更新到版本 6.0.2,现在我遇到了一些错误,这是我在更新我的应用程序之前没有发现的。 我有一个从 IdentityDbContext 类继
我正在处理一个大型项目,每个 DbContext 下有 50 多个 DbContext 和 100 多个 DbSet。 每个 DbContext 都由一个单独的团队处理,他们根据他们的要求/更改添加/
我是 WPF 的初学者。我想知道 dbcontext.Add 和 dbcontext.AddObject 之间有什么区别。 private void AddButton_Click(object se
我正在使用 MVC .Net。通常,每当我需要查询数据库时,我总是使用类似下面的方法来创建一个实例: using (EntitiesContext ctx = new EntitiesContext(
我在 HomeController 中有一个方法,我试图通过 URL 访问它,如下所示: http://localhost/web/home/GetSmth 第一次工作,但刷新页面后,我收到此错误:
在我的 Controller 中,我有这个 private readonly DbContext _context; public CountryController(DbContext contex
我正在寻找一种回滚实体更改的方法。我遇到了this answer它显示了如何设置实体状态,但我想知道如果我只是处理我的 dbContext 实例而不调用 dbContext.SaveChanges()
在我的项目中,我使用entity framework 7 和asp.net mvc 6\asp.net 5。我想为自己的模型创建 CRUD 我怎样才能做得更好: 使用 Controller 中的 db
我正在使用 Asp.Net Core 2.1 开发 Web 应用程序。在我使用脚手架添加新身份后,它为我生成了这些代码: IdentityStartup.cs 中生成的代码 [assembly:Hos
一旦我解决了one issue与 IBM.EntityFrameworkCore ,另一个出现了。对于 DB2 和他们的 .NET 团队来说,一切都是那么艰难和痛苦...... 问题:我在同一个 VS
我正在尝试创建一个播种用户和角色数据的类。 我的播种数据类(class)采用RoleManager构造函数参数 public class IdentityDataSeeder { private
我正在使用 .NET Core 2.1 构建 Web API。这将作为 Azure Web 应用程序托管。我想将数据库连接字符串保留在 Azure Key Vault 中。这是我放入 Startup.
当使用像 MySQL 这样的网络数据库时,DbContext 应该是短暂的,但是根据 https://www.entityframeworktutorial.net/EntityFramework4.
我有一个直接调用的 Controller 操作,但抛出了这个错误: The operation cannot be completed because the DbContext has been d
我在 Visual Studio 中使用默认的 ASP.Net MVC 模板。我正在使用在模板中为我创建的 ASP.Net 身份代码。我希望我使用的 DBContext 了解 ApplicationU
我有一个软件产品,它的数据库是在 SQLServer 上创建的,表名和列名是由开发团队定义的,然后使用 Database First 方法将模型导入 Visual Studio,现在我们正在为其他公司
我正在使用 EFCore 加载“用户”实体和用户制作的相关“订单”。 我有一个构造函数(来自真实代码的简化示例),它使用 id=1 加载用户并实现一个命令来更新 LoadedUser 实体中的更改。但
我是一名优秀的程序员,十分优秀!