While changing the object context, an error occurred.

.net c# entity-framework exception-handling

Question

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

1
6
6/7/2012 6:03:41 PM

Accepted Answer

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.

6
4/13/2011 9:35:54 PM

Popular Answer

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;


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