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

Updating Entity fails #867


User avatar
0
shaileshbc created

i am using EventCloud application ( latest downloaded from github) and added one or more entity. Based on event entity, i have created CRUD for my entities and they gets updated when their is no validation on entity, but fails if any validation is done on entity in Core project. Validation - checking whether one of the field is not getting duplicated before updating. It tries to add entity rather than updating entity. so how to solve, this issue.


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Can you share some of your code. Are you getting any exception? Share it also.

  • User Avatar
    0
    shaileshbc created
    public async Task UpdateAsync(Guard guard)
            {
                //Guard updateGuard = Guard.Create
                //(guard.SiteId,guard.GuardNo,guard.GuardName,guard.CardNo,guard.IsActive,guard.Id);
                //Guard _guard = await GetGuardUsingGuardNoAsync(updateGuard.GuardNo);
                //if (_guard != null && _guard.Id != updateGuard.Id)
                //    throw new UserFriendlyException("Guard No. already Exists!");
                //_guard = await GetGuardUsingCardNoAsync(guard.CardNo);
                //if (_guard != null && _guard.Id != guard.Id)
                //    throw new UserFriendlyException("Card No. already Exists!");
                await _guardRepository.UpdateAsync(guard);
            }
    

    if i uncomment the code and run it, then while saving entity i get : System.InvalidOperationException: Attaching an entity of type 'GTMS.Domain.Guard' failed because another entity of the same type already has the same primary key value.

    here primary key is ID and it is autonumber.

    Commented Code checks for duplication of field value. with above coding, software can update the entity, but when uncommented, it gives above error. regards Shailesh

  • User Avatar
    0
    hikalkan created
    Support Team

    This is related to EF. You're using the ORM worngly. You're getting an entity but updating another entity with same Id.

    1. General update code should like this:
    • Get entity from database
    • Change/override properties

    Even no need to call repository.Update. Example: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Application-Services#UowAutoSave">http://www.aspnetboilerplate.com/Pages/ ... owAutoSave</a>

    1. Is that an application service method. If so, follow the best practices and receive DTO (<a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Data-Transfer-Objects">http://www.aspnetboilerplate.com/Pages/ ... er-Objects</a>) as argument instead of receiving entity.
  • User Avatar
    0
    shaileshbc created

    Dear hikalkan, Thank you for quick response. i will check and in case of any issue, will get back to you.

    The code is from domain service, hence used actual entity rather than dto.

    regards Shailesh