You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Possibly you could define extension methods (or extension members when those are released) on the DbContext type to get and set the session token on the DbContext before any reads or writes.
I would like to work on this if it doesn't turn out too compex.
Depending on the outcome of #17308 we might need a different api to set and retrieve the session token.
Session tokens are unique for each 'write action' in a collection and partition key. This write action could be an CreateItem/ReplaceItem or it could be a TransactionalBatch. The original suggested api would not make sense as the collection and partition key might differ per entity or entity type, and currently (without transactional batch) it will always be different per entity, as each change will be a unique CreateItem/ReplaceItem call.
We could add an extension method to IQueryable to set the session token and to EntityEntry to retrieve the session token.
But being able to set the session token for all queries executed on the context instance would also be nice.
varresult=awaitmyDbContext.Documents.WithSessionToken(sessionToken).Where(x =>x.Id==id).FirstOrDefaultAsync();result.Property="changes";awaitmyDbContext.SaveChangesAsync();sessionToken=myDbContext.Entry(result).GetSessionToken();myDbContext.SetSessionTokenFor(x =>x.Documents,sessionToken);myDbContext.SetSessionTokenFor<Document>(sessionToken);varresult=awaitmyDbContext.Documents.Where(x =>x.Id==id).FirstOrDefaultAsync();// WithSessionToken not needed
However, if the team decides to implement #17308 (TransactionalBatch) all entries in the same batch will have the same session token. An api that reflects that could also make sense
Managing session tokens manually is a pretty advanced scenario and managing multiple session tokens per unit-of-work would be even more of a corner case, that would complicate the API beyond the value it would provide.
I think that a reasonable compromise is to add API similar to the proposed in the first comment:
GetSessionToken() would throw if more than one session token has been returned since this context instance was created or leased from the pool or SetSessionToken(null) was called. This should work nicely with the atomicity exceptions enabled from #17308
@AndriySvyryd I see what you’re saying, but in my experience managing session tokens manually is usually a common requirement rather than an “advanced” feature. To my understanding, horizontally scaled web applications that don’t use session affinity generally need some form of session token management to guarantee read-your-own-writes. Also, unless all documents in a given context are partitioned the same and stored in the same container, users might end up with multiple session tokens within a unit-of-work. Using AutoTransactionBehavior.Always ensures the former, though it’s not the default.
Also, unless all documents in a given context are partitioned the same and stored in the same container, users might end up with multiple session tokens within a unit-of-work.
Even so, we would need to create something that's more user-friendly. For example, the object returned by dbContext.Database.GetSessionToken() could be something like a ConcurrentDictionary<Grouping, string>. That way, the user doesn't need to handle this for each collection.
Just for my understanding, in which scenarios is it important for user code to be interacting with session tokens via the APIs proposed above? At least in an ideal world, shouldn't EF be managing session tokens internally, automatically providing read-your-writes behavior without the user having to manually read and set session tokens? In other words, I'd imagine a more high-level behavior where there's an EF enum setting managing the consistency level, with EF simply applying it under the hood.
Note that I'm not arguing against possibly also introducing a low-level API to manually manage session tokens (as proposed above); that might be necessary for really advanced/rare scenarios where users need to extract the session token in order to use it outside EF. But at least in the very common case, it doesn't feel like users should need to concern themselves with this and manually write such low-level code just to get read-your-writes behavior.
Even so, we would need to create something that's more user-friendly. For example, the object returned by dbContext.Database.GetSessionToken() could be something like a ConcurrentDictionary<Grouping, string>. That way, the user doesn't need to handle this for each collection.
@AndriySvyryd That sounds like a better, simpler solution indeed! Would it be ok to expose the cosmos db sdk type PartitionKey in the grouping? I think in that case we would also need something like dbContext.Database.SetSessionToken(Grouping, string) to set the session token. And what are your thoughts on IQueryable.WithSessionToken(string) extension method? We could also implement both dbContext.Database.GetSessionToken**s**() and dbContext.Database.GetSessionToke**n**() in which the latter would throw if there are multiple session tokens available for the last SaveChanges.
Just for my understanding, in which scenarios is it important for user code to be interacting with session tokens via the APIs proposed above? At least in an ideal world, shouldn't EF be managing session tokens internally, automatically providing read-your-writes behavior without the user having to manually read and set session tokens?
However, since it is only in-memory, a distributed or horizontally scaled application that is not using session-affinity will have to implement some form of manual session token management to guarantee read-your-own-writes.
I don't follow (or you might have misunderstood my suggestion)... I understand that when interacting directly with the Cosmos SDK, one must manually manage session tokens. My question is why we need to surface that low-level management to EF Core users, rather than letting EF manage that for them. In other words, unless there's some specific technical blocker, I'd expect the EF provider would internally get the session tokens from the Cosmos SDK and set them back, providing users with a seamless read-what-you-wrote experience without them having to know anything about session tokens (of course, it might make sense to make this an opt-in behavior e.g. for performance reasons).
My question is why we need to surface that low-level management to EF Core users, rather than letting EF manage that for them.
Sorry for the misunderstanding! @roji
In my experience, session token management is often implemented using cookies. This is also suggested in the [docs I linked earlier] (https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-manage-consistency?tabs=portal%2Cdotnetv2%2Capi-async#utilize-session-tokens). This seems to me like a technical blocker, as we don't know which presentation framework/logic the user is using outside EF. Unless there is a EntityFrameworkCore.Aspnet integration package I am not aware of, we can not implement it for the user. And even if we would choose to publish such a package, it would need to leverage some of the suggested api's.
However, there are other ways a user might choose to implement the manual session token management (without session affinity). One option is using a distributed cache to store stateful sessions, but you would also need some cookie or token to be sent with the request to identify the session of the user, just like the manual cosmos session token management. I assumed (maybe incorrectly) that that would be beyond the scope of EF Core. We can not use cosmos as this distributed cache as we would need read-your-own-write guarantees in order for it to function.
@roji In other words, to my understanding, in a distributed web application the session token retrieved from Cosmos DB must be managed (indirectly) by the user’s client code.
@JoasE thanks, that makes sense. For a distributed/disconnected scenario, where an update done via e.g. one web REST call needs to be visible from another, you're right that you'd need some means to extract the session token in order to pass it around.
So there are potentially two separate work items here:
A simpler, built-in mechanism where EF manages session tokens for you under the hood. This can be used in simpler, non-distributed where there's just one DbContext in use.
A more advanced/low-level mechanism where the session token can be manually extracted and set by the user (what we've been discussing above).
What about if EF only sends session tokens along with the request if the user has invoked context.Database.UseSessionTokens(Dictionary<string,string>) instead of globally enabling? EF would still always track the tokens (retrievable via GetSessionTokens), but would only send them if UseSessionTokens was called. As long as the app hasn’t explicitly provided tokens, EF would continue using automatic session token management.
This sounds like a good compromise. This discussion makes me think that EF session token management behavior should be configured with an enum:
FullyAutomatic (default) - Just the automatic token management in CosmosClient. GetSessionTokens and UseSessionTokens throw when invoked. GetSessionTokens could be made to not throw later, if appropriate CosmosClient API is added.
SemiAutomatic - As described above, session tokens only replaced if UseSessionTokens was called. Later overloads of UseSessionTokens and AppendSessionTokens can be added that don't replace the automatic CosmosClient session tokens, if appropriate CosmosClient API is added.
Manual - Session tokens are tracked, but only the ones specified in UseSessionTokens are sent. If UseSessionTokens wasn't called then use an empty string to remove the CosmosClient automatic session tokens from the request.
EnforcedManual - Same as Manual but throws if UseSessionTokens wasn't called before a request.
Names subject to change. The last two can be implemented in a separate PR.
Manual - Session tokens are tracked, but only the ones specified in UseSessionTokens are sent. If UseSessionTokens wasn't called then use an empty string to remove the CosmosClient automatic session tokens from the request.
Do you mean that EF won't update the tokens used for subsequent queries after a read/write? I'm not sure in which case that would be usefull. And if the user would want this, I think EF could instead offer a way to overwrite session tokens, like the IQueryable extension method I mentioned earlier: WithSessionToken. Use-/SetSessionTokens would probably also work for this, the user would have to invoke this after every read/savechanges tho.
If you want to do this to allow the user to query without any session tokens (only the empty string): That sounds a bit like the equivalent of QueryRequestOptions.ConsistencyLevel = ConsistentPrefix (Or Eventual), which could be another feature (IQueryable extension method or ConsistencyLevel property on IDatabase). I am not 100% sure they would behave excatly the same, but both sound like: Give me the first data you find for this query.
EnforcedManual - Same as Manual but throws if UseSessionTokens wasn't called before a request.
This sounds like it could also be a configurable warning.
Do you mean that EF won't update the tokens used for subsequent queries after a read/write?
No, it will update the tokens but only use them if UseSessionTokens has been called. This is for the use case you were describing where the app is fully controlling the session tokens. The reason I think that automatic session tokens shouldn't be used in this scenario is to have a consistent behavior, so if the app has a bug where the session token is not set then it will always result in old data instead of sometimes working correctly if it happens to get the same CosmosClient instance, this is the sort of issues that only happen in production, so we need to help developers catch them as soon as possible.
This is half-way to EnforcedManual, which I think would be the recommended setting for production with manual session tokens, but would allow requests without any overhead of session tokens. Perhaps if we add QueryRequestOptions.ConsistencyLevel support this would be redundant, I also am not quite sure whether it will behave in the same way
Activity
JoasE commentedon Aug 17, 2025
I would like to work on this if it doesn't turn out too compex.
Depending on the outcome of #17308 we might need a different api to set and retrieve the session token.
Session tokens are unique for each 'write action' in a collection and partition key. This write action could be an CreateItem/ReplaceItem or it could be a TransactionalBatch. The original suggested api would not make sense as the collection and partition key might differ per entity or entity type, and currently (without transactional batch) it will always be different per entity, as each change will be a unique CreateItem/ReplaceItem call.
We could add an extension method to
IQueryableto set the session token and toEntityEntryto retrieve the session token.But being able to set the session token for all queries executed on the context instance would also be nice.
However, if the team decides to implement #17308 (TransactionalBatch) all entries in the same batch will have the same session token. An api that reflects that could also make sense
Input from the team is greatly appreciated :)
AndriySvyryd commentedon Aug 18, 2025
Managing session tokens manually is a pretty advanced scenario and managing multiple session tokens per unit-of-work would be even more of a corner case, that would complicate the API beyond the value it would provide.
I think that a reasonable compromise is to add API similar to the proposed in the first comment:
GetSessionToken()would throw if more than one session token has been returned since this context instance was created or leased from the pool orSetSessionToken(null)was called. This should work nicely with the atomicity exceptions enabled from #17308JoasE commentedon Sep 11, 2025
@AndriySvyryd I see what you’re saying, but in my experience managing session tokens manually is usually a common requirement rather than an “advanced” feature. To my understanding, horizontally scaled web applications that don’t use session affinity generally need some form of session token management to guarantee read-your-own-writes. Also, unless all documents in a given context are partitioned the same and stored in the same container, users might end up with multiple session tokens within a unit-of-work. Using AutoTransactionBehavior.Always ensures the former, though it’s not the default.
AndriySvyryd commentedon Sep 12, 2025
Even so, we would need to create something that's more user-friendly. For example, the object returned by
dbContext.Database.GetSessionToken()could be something like aConcurrentDictionary<Grouping, string>. That way, the user doesn't need to handle this for each collection.roji commentedon Sep 12, 2025
Just for my understanding, in which scenarios is it important for user code to be interacting with session tokens via the APIs proposed above? At least in an ideal world, shouldn't EF be managing session tokens internally, automatically providing read-your-writes behavior without the user having to manually read and set session tokens? In other words, I'd imagine a more high-level behavior where there's an EF enum setting managing the consistency level, with EF simply applying it under the hood.
Note that I'm not arguing against possibly also introducing a low-level API to manually manage session tokens (as proposed above); that might be necessary for really advanced/rare scenarios where users need to extract the session token in order to use it outside EF. But at least in the very common case, it doesn't feel like users should need to concern themselves with this and manually write such low-level code just to get read-your-writes behavior.
JoasE commentedon Sep 12, 2025
@AndriySvyryd That sounds like a better, simpler solution indeed! Would it be ok to expose the cosmos db sdk type
PartitionKeyin the grouping? I think in that case we would also need something like dbContext.Database.SetSessionToken(Grouping, string) to set the session token. And what are your thoughts onIQueryable.WithSessionToken(string)extension method? We could also implement bothdbContext.Database.GetSessionToken**s**()anddbContext.Database.GetSessionToke**n**()in which the latter would throw if there are multiple session tokens available for the last SaveChanges.@roji I understand! And actually, the azure cosmos db sdk does this for us in-memory already! However, since it is only in-memory, a distributed or horizontally scaled application that is not using session-affinity will have to implement some form of manual session token management to guarantee read-your-own-writes.
See: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-manage-consistency?tabs=portal%2Cdotnetv2%2Capi-async#utilize-session-tokens for more info
roji commentedon Sep 12, 2025
I don't follow (or you might have misunderstood my suggestion)... I understand that when interacting directly with the Cosmos SDK, one must manually manage session tokens. My question is why we need to surface that low-level management to EF Core users, rather than letting EF manage that for them. In other words, unless there's some specific technical blocker, I'd expect the EF provider would internally get the session tokens from the Cosmos SDK and set them back, providing users with a seamless read-what-you-wrote experience without them having to know anything about session tokens (of course, it might make sense to make this an opt-in behavior e.g. for performance reasons).
JoasE commentedon Sep 12, 2025
Sorry for the misunderstanding! @roji
In my experience, session token management is often implemented using cookies. This is also suggested in the [docs I linked earlier] (https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-manage-consistency?tabs=portal%2Cdotnetv2%2Capi-async#utilize-session-tokens). This seems to me like a technical blocker, as we don't know which presentation framework/logic the user is using outside EF. Unless there is a EntityFrameworkCore.Aspnet integration package I am not aware of, we can not implement it for the user. And even if we would choose to publish such a package, it would need to leverage some of the suggested api's.
However, there are other ways a user might choose to implement the manual session token management (without session affinity). One option is using a distributed cache to store stateful sessions, but you would also need some cookie or token to be sent with the request to identify the session of the user, just like the manual cosmos session token management. I assumed (maybe incorrectly) that that would be beyond the scope of EF Core. We can not use cosmos as this distributed cache as we would need read-your-own-write guarantees in order for it to function.
JoasE commentedon Sep 12, 2025
@roji In other words, to my understanding, in a distributed web application the session token retrieved from Cosmos DB must be managed (indirectly) by the user’s client code.
roji commentedon Sep 15, 2025
@JoasE thanks, that makes sense. For a distributed/disconnected scenario, where an update done via e.g. one web REST call needs to be visible from another, you're right that you'd need some means to extract the session token in order to pass it around.
So there are potentially two separate work items here:
We may want a separate issue for the 1st.
22 remaining items
AndriySvyryd commentedon Nov 5, 2025
This sounds like a good compromise. This discussion makes me think that EF session token management behavior should be configured with an enum:
CosmosClient.GetSessionTokensandUseSessionTokensthrow when invoked.GetSessionTokenscould be made to not throw later, if appropriate CosmosClient API is added.UseSessionTokenswas called. Later overloads ofUseSessionTokensandAppendSessionTokenscan be added that don't replace the automatic CosmosClient session tokens, if appropriate CosmosClient API is added.UseSessionTokensare sent. IfUseSessionTokenswasn't called then use an empty string to remove theCosmosClientautomatic session tokens from the request.UseSessionTokenswasn't called before a request.Names subject to change. The last two can be implemented in a separate PR.
JoasE commentedon Nov 5, 2025
@AndriySvyryd
Do you mean that EF won't update the tokens used for subsequent queries after a read/write? I'm not sure in which case that would be usefull. And if the user would want this, I think EF could instead offer a way to overwrite session tokens, like the IQueryable extension method I mentioned earlier:
WithSessionToken. Use-/SetSessionTokens would probably also work for this, the user would have to invoke this after every read/savechanges tho.If you want to do this to allow the user to query without any session tokens (only the empty string): That sounds a bit like the equivalent of QueryRequestOptions.ConsistencyLevel = ConsistentPrefix (Or Eventual), which could be another feature (IQueryable extension method or ConsistencyLevel property on IDatabase). I am not 100% sure they would behave excatly the same, but both sound like: Give me the first data you find for this query.
This sounds like it could also be a configurable warning.
AndriySvyryd commentedon Nov 5, 2025
No, it will update the tokens but only use them if
UseSessionTokenshas been called. This is for the use case you were describing where the app is fully controlling the session tokens. The reason I think that automatic session tokens shouldn't be used in this scenario is to have a consistent behavior, so if the app has a bug where the session token is not set then it will always result in old data instead of sometimes working correctly if it happens to get the same CosmosClient instance, this is the sort of issues that only happen in production, so we need to help developers catch them as soon as possible.This is half-way to EnforcedManual, which I think would be the recommended setting for production with manual session tokens, but would allow requests without any overhead of session tokens. Perhaps if we add QueryRequestOptions.ConsistencyLevel support this would be redundant, I also am not quite sure whether it will behave in the same way
JoasE commentedon Nov 10, 2025
@AndriySvyryd I think I understand what you mean, tho I am a bit confused on this part
I think EF should use the session token it got from a write or read when using manual. But I think you also mean that.
So:
or
Merge branch 'main' of https://github.com/JoasE/efcore into feature/d…
Add Cosmos db manual session token management (#37027)
Document Cosmos session token management
Preview 1 docs (#5256)
Preview 1 docs (#5256)