error occurred while updating the object context
Solution 1
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.
Solution 2
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);
}
Solution 3
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;
Nadeem Khedr
I am a programmer who loves web development & consider it both a job & a hobby. Most of my background is .NET but for sometime now I have transitioned to the dynamic realm of programming (loving Javascript/Node/Ruby) Most of my work nowadays is making SPA apps. Worked mainly with Ember, Angular, Ionic, Backbone, Knockout & Firebase In my free time I play around with vim, bash, zsh & trying to find new tips/tricks to make myself more productive I blog & tweet often about new interesting technologies or problems that I find.
Updated on June 20, 2022Comments
-
Nadeem Khedr almost 2 years
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
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 -
justin.lovell over 10 yearsWould the
context.Users.Add(user)
bit be allowing EF to handle the whole object graph?