ObjectStateManager中已经存在具有相同密钥的对象。 ObjectStateManager无法跟踪具有相同键的多个对象 [英] An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

查看:201
本文介绍了ObjectStateManager中已经存在具有相同密钥的对象。 ObjectStateManager无法跟踪具有相同键的多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个表,其中包含一些公司的几个属性。这是主表,其ID在许多其他表中使用。我基本上通过这种方法找到自己的ID:

Basically, I have a table which contains a few properties for a company. This is the "master" table and their ID is used in many other tables. I basically find their ID via this method:

private Company currentcompany()
    {
        Company cuco = db.Companies.Single(x => x.username == User.Identity.Name);
        return cuco;
    }

我需要给用户更新关于自己存储的各种细节表,我做得很好 - 但是,我注意到一个很大的安全漏洞!

I need to give users the ability to update various details about themselves stored in this table, which I did perfectly well - however, I noticed a big security hole!

在Firefox上使用篡改数据(我想象Fidler /许多其他人),我可以轻松更改隐藏ID并修改其他公司的详细信息。

Using Tamper Data on Firefox (And I imagine Fidler/many others), I could easily change the hidden ID and modify another companies details.

为了阻止这一点,我将以下行添加到修改操作中:

To stop this, I added the following lines to the modify action:

        Company cuco = currentcompany();

        if (company.id != cuco.id)
        {
            return Content("Security Error");
        }

(FYI - 公司是代表公司的模型/ POCO,而公司本身是表单数据。)

(FYI - Company is a model/POCO representing a company, and company itself is the form data.)

添加后如果我编辑表单数据中的ID,它将按预期工作并显示安全错误,但是,如果没有错误,我继续,我得到问题的错误。

After adding this, if I edit the ID in the form data, it works as expected and brings up "Security Error", however, if there isn't an error and I go on, I get the error in the question.

ObjectStateManager中已经存在具有相同密钥的对象,ObjectStateManager无法跟踪具有相同键的多个对象。

"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."

我相信这是因为EF是以某种方式检测和保持第一个数据拉,但我不知道如何纠正它。

I believe this is because EF is somehow detecting and keeping the first data pull, but I am just un sure on how to correct it.

任何建议?

edit-
--update -

edit- --update--

如果你能明白我想要实现的,是否有一个更好的方法来解决这个问题?

If you can understand what I am trying to achieve, is there a better way of going around this?

推荐答案

如果从上下文加载实体,则不能附加具有相同ke再次。第一个实体仍然保留在内部上下文缓存中,上下文只能容纳一个具有给定键值的实例,它被称为身份映射和我在这里描述了在其他情况)。

If you load the entity from the context you cannot attach an entity with the same key again. The first entity is still kept in internal context cache and context can hold only one instance with given key value per type (it is called identity map and I described it here in other situation).

你可以通过分离前一个实例来解决它,但你不用了如果你只需要保存新的值就可以使用这个:

You can solve it by detaching former instance but you don't have to. If you only need to save new values you can use this:


  • ObjectContext API: context.YourEntitySet.ApplyCurrentValues( newEntity);

  • DbContext API: context.Entry(oldEntity).CurrentValues.SetValues(newEntity);

  • ObjectContext API: context.YourEntitySet.ApplyCurrentValues(newEntity);
  • DbContext API: context.Entry(oldEntity).CurrentValues.SetValues(newEntity);

这篇关于ObjectStateManager中已经存在具有相同密钥的对象。 ObjectStateManager无法跟踪具有相同键的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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