locked
Get changes to user profile to persist in user table. RRS feed

  • Question

  • User-1035489207 posted

    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
    manager1.UpdateAsync(currentUser)
    store1.Context.SaveChangesAsync()
    

    I want to be able to change the subscription status of a user and have those changes remain in the user table.

    Sunday, June 23, 2019 12:49 PM

All replies

  • User753101303 posted

    Hi,

    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 :

    await manager1.UpdateAsync(currentUser)
    Monday, June 24, 2019 7:38 AM
  • User665608656 posted

    Hi JamberFX,

    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 

    JamberFX

    store1.Context.SaveChangesAsync()

    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

    Best Regards,

    YongQing.

    Monday, June 24, 2019 9:46 AM
  • User-1035489207 posted

    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.

    store1.Context.SaveChangesAsync()
    Monday, June 24, 2019 1:52 PM
  • User753101303 posted

    IMHO you should really use UserManager rather than directly the underlying store and call async methods using await...

    Monday, June 24, 2019 1:59 PM
  • User-1035489207 posted

    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.

    Monday, June 24, 2019 2:07 PM
  • User665608656 posted

    Hi JamberFX,

    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:

    await manager1.UpdateAsync(currentUser)

    For more details, you could refer to this link about await: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/await

    Best Regards,

    YongQing.

    Tuesday, June 25, 2019 8:31 AM
  • User753101303 posted

    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.

    Tuesday, June 25, 2019 8:43 AM