Base solution for your next web application
Open Closed

Changing UserRole Table to Fully Audited #3553


User avatar
0
soonjoo created

Hello, I changed all my foreign keys to delete restrict instead of delete cascade because delete cascade was giving issues during migrations of my new entities. (I have some parent child recursive referencing and they wont allow me to delete cascade)

However, now running the tests failed for Update_User_Basic_Tests and Should_Remove_From_Role.

Message: System.InvalidOperationException : The association between entity types 'User' and 'UserRole' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.

I was thinking of changing the table to fully audited so it doesn't actually delete the record and prevent throwing this error? Or have any other way to solve this issue? Thanks


11 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I couldn't think of a better solution at the moment. I suggest you to test your application when you did this change carefully.

    Thanks for the suggestion, I think it will help others.

  • User Avatar
    0
    ulaskurt created

    Hello ismcagdas, I'm getting this error. I get the same error when I want to remove the role in the user update. Do you have any suggestions Solution? The system is currently running live, i can not perform table changes.

    Regards.

  • User Avatar
    0
    alper created
    Support Team

    Might have forgotten to attach the error screenshot ?

  • User Avatar
    0
    ulaskurt created

    The error message is as follows. I understand that the UserRole table should be FullAudited.


    INFO 2018-04-16 21:45:44,053 [3 ] uthentication.JwtBearer.JwtBearerHandler - AuthenticationScheme: Bearer was successfully authenticated. INFO 2018-04-16 21:45:44,069 [3 ] pNetCore.Cors.Infrastructure.CorsService - Policy execution successful. INFO 2018-04-16 21:45:44,116 [3 ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method Ruby.Authorization.Users.UserAppService.CreateOrUpdateUser (Ruby.Application) with arguments (Ruby.Authorization.Users.Dto.CreateOrUpdateUserInput) - ModelState is Valid ERROR 2018-04-16 21:45:44,190 [10 ] Mvc.ExceptionHandling.AbpExceptionFilter - The association between entity types 'User' and 'UserRole' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes. System.InvalidOperationException: The association between entity types 'User' and 'UserRole' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes. at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleConceptualNulls() at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave() at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__59.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__48.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.EntityFrameworkCore.AbpDbContext.<SaveChangesAsync>d__49.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Zero.EntityFrameworkCore.AbpZeroCommonDbContext3.<SaveChangesAsync>d__98.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.<SaveChangesInDbContextAsync>d__20.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.<SaveChangesAsync>d__12.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Authorization.Users.AbpUserStore2.<UpdateAsync>d__36.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Identity.UserManager1.<UpdateUserAsync>d__171.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Identity.UserManager1.<RemoveFromRoleAsync>d__106.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Authorization.Users.AbpUserManager`2.

  • User Avatar
    0
    alper created
    Support Team

    check out the related issue in Entity Framework repository <a class="postlink" href="https://github.com/aspnet/EntityFrameworkCore/issues/9466#issuecomment-323519562">https://github.com/aspnet/EntityFramewo ... -323519562</a>

  • User Avatar
    0
    ulaskurt created

    Thank you for your interest. The corresponding code block is in the Entity. But the solution was not. I just get this error in the UserRole table. I think that the problem will be solved when the IsDeleted field is added. Because there is no problem adding and deleting. We only get this error in the role update.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @UlasKurt,

    Can you move base.OnModelCreating(modelBuilder); to the first line of OnModelCreating ?

  • User Avatar
    0
    ulaskurt created

    Hi @ismcagdas

    Thanks for your suggestion, but the error continues. I think the UserRole table should have the IsDeleted field. But as far as I can see from the documents, i can not access UserManagement.

    Do you have another suggestion?

    Regards.

  • User Avatar
    0
    ismcagdas created
    Support Team

    @UlasKurt,

    You cannot change UserRole to SoftDelete. So, you can check if the entity type implements ISoftDelete while setting DeleteBehaviour of the relationship and don't set it to Restrict if entity type implements ISoftDelete interface.

  • User Avatar
    0
    ulaskurt created

    Thanks everyone.

    I realized the solution by separating the related table from the relations. For example:

  • User Avatar
    0
    alper created
    Support Team

    thanks for your feedback ;)