Error trying to update with ASP.NET MVC5 entity framework

0

I have the following problem when I try to update the Gender object, it shows me the following error when I do the update, help

    try
    {
        Common.Entities.Gender gender = this.genderRepository.FindGenderId(genderModel.GenderId);

        Domain.Models.Gender myGender = new Domain.Models.Gender
        {
            GenderId = gender.GenderId,
            GenderName = genderModel.GenderName,
            CreateBy = gender.CreateBy,
            CreationDate = gender.CreationDate,
            ModifiedBy = genderModel.ModifiedBy,
            ModifiedDate = genderModel.ModifiedDate,
        };

        this.genderRepository.UpdateGender(myGender);
        serviceResult.Data = "Genero modificado.";
    }
    catch (Exception ex)
    {
        serviceResult.Data = "Ocurrio un error modificado el genero.";
        serviceResult.RegisterError(ex);
    }

    return serviceResult;
}

and this is the objecto that I believe so that I update the data to make the update

public void UpdateGender(Models.Gender myGender)
{

    this.unitOfWorkModel.Repository<Gender>().Update(new Gender
    {
        GenderId = myGender.GenderId,
        GenderName = myGender.GenderName,
        CreateBy = myGender.CreateBy,
        CreationDate = myGender.CreationDate,
        ModifiedBy = myGender.ModifiedBy,
        ModifiedDate = myGender.ModifiedDate,
    });

    this.unitOfWorkModel.Commit();
    this.unitOfWorkModel.Dispose();
}

This is the image, with the error.

    
asked by Ing. Jose Valera 04.01.2019 в 14:24
source

1 answer

0

One of the solutions is that you make the Select (Method to select the records) before the update. What you will achieve is that the registry is cached when it is found. You must add AsNoTracking() in the method you are using to select the data.

    
answered by 04.01.2019 в 14:51