error occurred while updating the object context

♀尐吖头ヾ 提交于 2019-12-05 08:20:47

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.

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);
}
Lucas

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;

in the database i set the relation to cascade on delete and update

1) I believe that if you setup cascading delete directly in the database you also need to define it in your model. The settings in the model designer are in the properties window of the relevant association (click on the association line between the two entities in the designer surface, then select "Properties").

2) I also believe that cascade on update is not supported in EF. Try to remove cascade on update in the database and check if it works then.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!