EF 6 error occurred while updating the object context

c# entity-framework entity-framework-6

Question

I own an entity.

public class User : BaseEntity, IEntity
{
    public string UserName { get; set; }

    public string Avatar { get; set; }

    [IgnoreMap]
    public string Password { get; set; }

    public int RoleId { get; set; }

    public virtual Role Role { get; set; }

    public string RoleName { get; set; }

    public int RangId { get; set; }

    public string RangName { get; set; }

    public string Email { get; set; }

    public string LastName { get; set; }

    public string FirstName { get; set; }

    public string SurName { get; set; }

    public DateTime? Birthday { get; set; }

    public int Gender { get; set; }

    public int? CountryId { get; set; }

    public virtual Country Country { get; set; }

    public int? CityId { get; set; }

    public virtual City City { get; set; }

    public string Skype { get; set; }

    public string WebSite { get; set; }

    public string Interest { get; set; }

    public string Signature { get; set; }

    public DateTime DateRegistered { get; set; }

    public string IpAddress { get; set; }

    public string PasswordChange { get; set; }

    public Guid? ActivateKey { get; set; }

    public DateTime? DatePasswordChange { get; set; }

    public int TotalPost { get; set; }

    public string NewEmail { get; set; }

I attempt to update a user's role.

var user = this._userRepository.GetEntity(userId);
user.RoleId = role.Id;

Attempting to change the navigation property to null

user.Role = null;

I attempt to set the new role's navigation attribute.

user.Role = role;

But I constantly make mistakes.

Additional information: The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: A referential integrity constraint violation occurred: The property value(s) of 'Role.Id' on one end of a relationship do not match the property value(s) of 'User.RoleId' on the other end.

I'm using for saving date

this._context.Entry(entity).State = EntityState.Modified;

UPDATE:

I learned a fascinating information. I can change my user role sometimes, but other times I can't. Examples

I can alter a user's role once they activate their accounts.

I am unable to alter a user's role after an email change.

I nearly always use the same code.

Update

Code for user activation is effective.

    public bool ActivateUser(Guid key)
    {
        var user = this._userRepository.GetEntity(item => item.ActivateKey == key);
        var role = this._roleRepository.GetEntity(item => item.RoleType == RoleType.User);

        if (user != null)
        {
            user.ActivateKey = null;
            user.RoleId = role.Id;
            this._userRepository.UpdateEntity(user);
            this._userRepository.SaveChanges();

            return true;
        }

        return false;
    }

Code for edit email user; I get an error

A referential integrity constraint violation occurred: The property value(s) of 'Role.Id' on one end of a relationship do not match the property value(s) of 'User.RoleId' on the other end.

    public void UpdateEmail(int userId, string newEmail, string browserAgent, string ip)
    {
        var user = this._userRepository.GetEntity(item => item.Id == userId);
        var role = this._roleRepository.GetEntity(item => item.RoleType == RoleType.NoActivation);
        var activateKey = Guid.NewGuid();

        var editEmailNotification = new EditEmailNotification
        {
            UserName = string.IsNullOrEmpty(user.FirstName) ? user.UserName : user.FirstName,
            OldEmail = user.Email,
            NewEmail = newEmail,
            Ip = ip,
            WebBrowseAgent = browserAgent,
            ActivateKey = activateKey
        };

        user.NewEmail = newEmail;
        user.ActivateKey = activateKey;
        user.RoleId = role.Id;
        this._userRepository.UpdateEntity(user);
        this._userRepository.SaveChanges();

        this.SendNotificationLetterChangedAccountEmail(editEmailNotification);
    }
1
0
7/16/2016 12:56:19 AM

Accepted Answer

ZZZ_tmp
0
7/16/2016 4:40:25 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow