I have 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 try update role of user
var user = this._userRepository.GetEntity(userId);
user.RoleId = role.Id;
I try set navigation property to null
user.Role = null;
I try set navigation property to new role
user.Role = role;
But I always get error
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.
For save date I'm using
this._context.Entry(entity).State = EntityState.Modified;
UPDATE:
I found interesting fact. Sometimes I can change user role, and sometimes I can't. Examples
When user activation his account, I can change user role.
When user change email, I can't change user role.
I using code almost the same
Update
Code for activation user work fine
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 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);
}