Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Validate for field unchanged on update #3404


User avatar
0
PhilWynn created

Hi,

I am implementing a ContactManager, where a Contact is linked to a Company. Once the Contact has been created, the company should no longer be allowed to change.

I have implemented the following method in ContactManager to achieve this validation:

protected async Task<IdentityResult> CheckCompanyUnchanged(int id, int companyId)
        {
            var contact = await _contactRepository.FirstOrDefaultAsync(c => c.Id == id);
            if (contact != null && contact.CompanyId != companyId)
            {
                return AbpIdentityResult.Failed(string.Format(L("ContactCompanyChanged")));
            }

            return IdentityResult.Success;
        }

The problem I am having is that when fetching the Contact to make the comparrison, it contains the updated company value rather than the original one due to entity tracking. Is there a way to access the original values from the Core project? I know this can be done via the dbContext, but have no access to this.


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

    Hi,

    Maybe, you can do it with a custom repository, it allows you to access DbContext in it <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Repositories#custom-repositories">https://aspnetboilerplate.com/Pages/Doc ... positories</a>.

  • User Avatar
    0
    PhilWynn created

    Thank you. Managed to find a solution using a custom repository.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)