Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error occurred while updating the object context

first of all here is the message

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 values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

the problem happens when i try to insert new data in the entityframework


My entity model

enter image description here

in the database i set the relation to cascade on delete and update. that is the only change i made to the relation


My Action Method :

[HttpPost]
    public ActionResult CompleteRegisteration(RegisterViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        var user = new User
                       {
                           DisplayName = model.DisplayName,
                           FullName = model.Name,
                           Email = model.Email,
                       };
        user.AuthenticationTokens.Add(new AuthenticationToken
                                          {
                                              ClaimedIdentifier = model.ClaimedIdentifier,
                                              DisplayName = model.Email
                                          });
        _userRepository.InsertOrUpdate(user);
        _userRepository.Save();

        return RedirectToAction("Index", "Home");
    }

and the user repository methods :

    private readonly StoryWritingEntities context = new StoryWritingEntities();

    public void InsertOrUpdate(User user)
    {
        context.Users.Attach(user);
        context.ObjectStateManager.ChangeObjectState(user,
                                                     user.Id == default(int)
                                                         ? EntityState.Added // if true then this is a new entry
                                                         : EntityState.Modified); // if false this is an Existing entry

    }
    public void Save()
    {
        context.SaveChanges();
    }

the problem is caused by context.SaveChanges() there is a record inserted in the users table but nothing is inserted in the AuthenticationTokens table

like image 921
Nadeem Khedr Avatar asked Apr 13 '11 21:04

Nadeem Khedr


3 Answers

If you simply did the following this wouldn't happen:

  context.Users.AddObject(user);
  content.SaveChanges();

I suspect the problem is occurring because EF doesn't know about the AuthenticationToken object, it's not being attached to the context because it's added to a disconnected entity which is then attached to the context.

You either need to let EF handle the whole object graph connectivity situation or you need to do it all yourself. Mixing and matching like this doesn't work.

like image 162
Ian Mercer Avatar answered Nov 20 '22 14:11

Ian Mercer


Try something different, like:

if(model.Id != null)
{
    UpdateModel(user);
}
else
{
    _userRepository.Insert(model)
}
_userRepository.Save();

And the _userRepository.Insert would be:

public void Insert(User user)
{
    context.Users.AddObject(user);
}
like image 1
Davidson Sousa Avatar answered Nov 20 '22 14:11

Davidson Sousa


I got this error because I was trying to edit a record/row in the table, but the code was adding a row with the same ID.

So I just changed the

ctx.table.Add(entity object);

too

ctx.Entry(entity object).State = EntityState.Modified;
like image 1
Lucas Avatar answered Nov 20 '22 15:11

Lucas