Error updating object context

here's the first message

The changes to the database were successful, but an error occurred while updating the context object. The object may be in an inconsistent state. Internal exception message: referential integrity violation of the constraint occurred: the values โ€‹โ€‹of the properties that define the referential constraints are not consistent between the principal and the dependent objects in relation.

the problem occurs when I try to insert new data in entityframework


My entity model

enter image description here

in the database, I set the cascade relation when deleting and updating. this is the only change I made for the relationship


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 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 because context.SaveChanges() has a record inserted in the users table, but nothing is inserted in the AuthenticationTokens table

+6
source share
4 answers

If you just did the following, this will not happen:

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

I suspect that the problem occurs because the EF does not know about the AuthenticationToken object, it is not context bound, because it is added to the disconnected object, which is then bound to the context.

You need to either allow EF to handle the whole situation of connecting to the object, or you need to do it all yourself. Mixing and matching like this does not work.

+6
source

Try something else, for example:

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

And _userRepository.Insert will be:

 public void Insert(User user) { context.Users.AddObject(user); } 
+1
source

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

So I just changed

 ctx.table.Add(entity object); 

too

 ctx.Entry(entity object).State = EntityState.Modified; 
+1
source

in the database, I set the cascade relation when deleting and updating

1) I believe that if you configure cascading deletion directly in the database, you also need to define it in your model. Parameters in the model constructor are in the properties window of the corresponding association (click on the association line between two objects on the surface of the constructor, then select "Properties").

2) I also think that cascade during upgrade is not supported in EF. Try to remove the cascade when updating in the database and check if it works then.

0
source

Source: https://habr.com/ru/post/885861/


All Articles