Base solution for your next web application
Open Closed

InsertOrUpdate Method Error #3112


User avatar
0
epro1 created

Hello,

I have problem with InsertOrUpdate method. I have created custom repository (I follow documentation). In that custom repository I have added one method where I get Entity by some parameters. The problem is now when I execute that method and then InsertOrUpdate method (from Base Repository) it gives me error:

Attaching an entity of type 'EntityName' failed because another entity of the same type already has the same primary key value.

Here is code:

public async Task Save(DetailsInput Input)
        {
            //check
            var country = _countryRepository.GetByPeriod(Input.DateFrom, Input.DateTo.Value);
            if (country != null && ((Input.Id.HasValue && taxRate.Id != Input.Id.Value) || !Input.Id.HasValue))
            {
                throw new UserFriendlyException(L("CountryForPeriodExists"));
            }
            
           Country  item  = Input.MapTo<Country>();
           await _countryRepository.InsertOrUpdateAsync(item);
            
        }

But if I remove that custom method and then try to execute InsertOrUpdate everthing is ok:

public async Task Save(DetailsInput Input)
        {
           Country  item  = Input.MapTo<Country>();
           await _countryRepository.InsertOrUpdateAsync(item);
            
        }

Is anyone know why this happens?

Thanks.


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

    Hi,

    I think the country you get here

    var country = _countryRepository.GetByPeriod(Input.DateFrom, Input.DateTo.Value);
    

    and your input dto has the same Id, am I right ?

    If so, you should change your country variable's fields and update it.