更新对象上下文时发生错误 [英] error occurred while updating the object context

查看:435
本文介绍了更新对象上下文时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是消息


对数据库的更改是
提交成功,但错误$ b在更新对象
上下文时发生$ b。 ObjectContext可能在
中是不一致的状态。内部异常
消息:引用完整性
约束违规发生:定义
参照约束的
属性值不是
在principal和
之间一致

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

我的实体模型

在数据库中,我将关联设置为级联删除和更新。这是我对关系的唯一改变

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

我的操作方法:

[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();
    }






问题是由 context.SaveChanges()在users表中插入一条记录,但在AuthenticationTokens表中没有插入任何内容


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

推荐答案

如果你只是做了以下这样的事情就不会发生:

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

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

我怀疑问题出现是因为EF不知道 AuthenticationToken 对象,它没有附加到上下文,因为它被添加到一个断开的实体,然后附加到上下文。

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.

你需要让EF处理整个对象图连接状况,或者你需要自己做。混合和匹配这样不起作用。

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.

这篇关于更新对象上下文时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆