Last post Jun 25, 2019 08:43 AM by PatriceSc
Jun 23, 2019 12:49 PM|JamberFX|LINK
When I make changes to a user field in the usermanager, I can't get those changes to persist in the user table by updating the userstore.
I have tried userstore.updateasync, userstore.savechanges, etc.
Dim dbcontext As New ApplicationDbContext
Dim store1 As New UserStore(Of ApplicationUser)(dbcontext)
Dim manager1 = New UserManager(Of ApplicationUser)(store1)
Dim currentUser = manager1.FindById(Context.User.Identity.GetUserId())
SubscriptionState = "Inactive"
currentUser.SubscriptStatus = SubscriptionState
I want to be able to change the subscription status of a user and have those changes remain in the user table.
Jun 24, 2019 07:38 AM|PatriceSc|LINK
See for example https://www.c-sharpcorner.com/article/async-await-and-asynchronous-programming-in-mvc/
You'll turn your method to something such as public async Task<AcrtionResult> YourMethod(etc...) and you'll call :
Jun 24, 2019 09:46 AM|Yongqing Yu|LINK
According to your question, do you have your own way to rewrite the ApplicationUser? If so, please provide more comprehensive relevant code.
UpdateAsync is an encapsulated method of updating Entity Framework database. It contains update and save operations, so
this statement can be omitted.
If you still can't update data successfully, you can use SQL Server profiler to check every SQL statement in your UpdateAsync method to see if the statement is correct.
How to use SQL Server profiler, you could refer to this link: https://docs.microsoft.com/en-us/sql/tools/sql-server-profiler/sql-server-profiler?view=sql-server-2017
Jun 24, 2019 01:52 PM|JamberFX|LINK
As it turns out, my code was actually working fine. However, where I had the code was not resulting in the database being updated because the user was not yet logged in. As @YongQuing points out, I can probably omit the line below and will try that later
and update here.
Jun 24, 2019 01:59 PM|PatriceSc|LINK
IMHO you should really use UserManager rather than directly the underlying store and call async methods using await...
Jun 24, 2019 02:07 PM|JamberFX|LINK
I am not real well versed in asynchronous programming but as I understand it, it would allow other tasks to go on before completing this task. Since this is a login page wouldn't that defeat the purpose of requiring the user to be "active" before logging
in or is there another reason to use asynchronous programming here.
Jun 25, 2019 08:31 AM|Yongqing Yu|LINK
According to your description, the UpdateAsync method itself is an asynchronous thread, which will continue to perform some of the following functions of the method while you perform the update operation.
So, for your issue, it's very likely that when you look at the updated data, the UpdateAsync method hasn't been updated yet.
As PatriceSc mentioned, in addition to deleting the last sentence, you also need to add await before the method to ensure that when you view the updated data, the update operation is complete like below:
For more details, you could refer to this link about await: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/await
Jun 25, 2019 08:43 AM|PatriceSc|LINK
await/async allows to trigger an operation and give back control so that the CPU could be used for other unrelated stuff such as processing other requests. Once the async operation is done your code resume execution where you await(ed) the async call.
The benefit is that it really looks like the sync version (unlike when using callbacks for example which are soon hard to follow).
So in short using await/async shouldn't cause any visible difference.