简体   繁体   English

EF Core Transient DBContext 获取旧值

[英]EF Core Transient DBContext get old value

I have a transient EF DB context & a transient service.我有一个瞬态 EF DB 上下文和一个瞬态服务。

I have a controller with an action using session service to update the session, and then use session service to query back the updated session. I have a controller with an action using session service to update the session, and then use session service to query back the updated session.

However, even I use transient, when I query back the updated session, the session is still the old value but not the updated one.但是,即使我使用瞬态,当我查询更新后的 session 时,session 仍然是旧值,但不是更新后的值。 Can someone answer why?有人可以回答为什么吗?

My expected behavior is if DBContext & SessionService are transient, get after my updated session should return a new updated value instead of old value.我的预期行为是,如果 DBContext 和 SessionService 是暂时的,在我更新后的 session 应该返回一个新的更新值而不是旧值。 Because DBContext should be disposed after the update.因为 DBContext 应该在更新后被释放。

Startup.cs启动.cs

public void ConfigureServices(IServiceCollection services)
{
    string connectionString = Configuration.GetConnectionString("ASPState");
    services.AddDbContext<ASPStateContext>(options =>
        options.UseSqlServer(connectionString, builder=> {
            builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
        }),ServiceLifetime.Transient);
    services.AddTransient<ISessionService, SessionService>();
    services.AddControllers();
}

SessionService.cs - using DI SessionService.cs - 使用 DI

private ASPStateContext aspStateContext;
public SessionService(ASPStateContext dbContext)
{
    aspStateContext = dbContext;
}

public async Task<IActionResult> UpdateSession(string id) {
    await this.sessionService.UpdateSession(id);
    var session = this.sessionService.GetSession(id); // return the value before update
}

ASPStateContext.cs ASPStateContext.cs

public partial class ASPStateContext : DbContext
{
    public ASPStateContext()
    {
    }

I think there may be a misunderstanding about how the Transient scope works.我认为 Transient scope 的工作原理可能存在误解。 A transient dependency is instantiated every time it is injected.每次注入时都会实例化瞬态依赖项。 Within your controller, a transient and a scoped dependency behave the same way.在您的 controller 中,瞬态和范围依赖的行为方式相同。 The difference emerges when another service depends on the transient service;当另一个服务依赖于瞬态服务时,差异就出现了; that other service will receive a new instance of the transient service.该其他服务将接收瞬态服务的新实例。

Therefore, your controller will use the same SessionService (and the same EF context) for an entire request.因此,您的 controller 将对整个请求使用相同的 SessionService(和相同的 EF 上下文)。

To ensure that Entity Framework is retrieving the latest value from the database, use the AsNoTracking query extension ( documentation here ).要确保实体框架从数据库中检索最新值,请使用AsNoTracking查询扩展( 此处的文档)。 This will completely bypass EF caching and query the underlying database.这将完全绕过 EF 缓存并查询底层数据库。

Also, make sure that your EF call to save changes after updating the session data is awaited.此外,请确保等待更新 session 数据后保存更改的 EF 调用。 Otherwise, your SELECT statement may execute before the UPDATE statement is applied.否则,您的 SELECT 语句可能会在应用 UPDATE 语句之前执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM