。具有相同键的一个目的已经存在于ObjectStateManagerObjectStateManager不能与相同的密钥跟踪多个对象 [英] . The An object with the same key already exists in the ObjectStateManagerObjectStateManager cannot track multiple objects with the same key

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

问题描述

我想简单地更新实体对象,我得到这个错误。所有对我做了错误的谷歌搜索带我到复杂的解释...谁能说得简单?

我工作的这个简单的教程

<一个href=\"http://aspalliance.com/1919_ASPNET_40_and_the_Entity_Framework_4__Part_2_Perform_CRUD_Operations_Using_the_Entity_Framework_4.5\" rel=\"nofollow\">http://aspalliance.com/1919_ASPNET_40_and_the_Entity_Framework_4__Part_2_Perform_CRUD_Operations_Using_the_Entity_Framework_4.5

 其他
                    {
                        // UPDATE
                        INT IFID = Int32.Parse(fid.First()fid.ToString());
                        oFinancial.fid = IFID;
                        oFinancial.mainqtr = currentQuarter;
                        oFinancial.mainyear = currentYear;
                        oFinancial.qtr = Int32.Parse(currentQuarter);
                        oFinancial.year = Int32.Parse(currentYear);
                        oFinancial.updatedate = DateTime.Now;
                        // ObjectStateEntry OSE = NULL;
                        //如果(!dc.ObjectStateManager.TryGetObjectStateEntry(oFinancial.EntityKey,出OSE))
                        // {
                        dc.financials.Attach(oFinancial);
                        //}                        dc.ObjectStateManager.ChangeObjectState(oFinancial,System.Data.EntityState.Modified);
                    }                    dc.SaveChanges();

这里是高达了code,我用简单更高,以获得我的主键值..可能是一个更好的办法,但它的工作原理。

  VAR FID从dc.financials X =
                  其中,iPhaseID == x.phaseid&放大器;&安培;
                         strTaskID == x.ftaskid&放大器;&安培;
                         strFundType == x.fundtype&放大器;&安培;
                         iCurrentQuarter == x.qtr&放大器;&安培;
                         iCurrentYear == x.year
                  选择X;


解决方案

如果在 oFinancial 对象从 DC 你永远手动分离,然后没有理由调用连接法或惹的 ObjectStateManager 。只要 DC 知道的对象(它,除非你分离的话),那么 ObjectStateManager 将保持跟踪您所做的任何更改并相应地更新它们,当你调用 dc.SaveChanges()

编辑:这里是你贴什么重构版本,希望它可以帮助:

 其他{
    // UPDATE
    //只要oFinancial从未detatched您检索后
    从DC//它,那么你就不必重新连接它。和
    //你不应该需要操作的主键,除非它是
    //不是由数据库生成的,你不已经有另一
    //对象中的DC具有相同的主键值。    INT IFID = Int32.Parse(fid.First()fid.ToString());
    oFinancial.fid = IFID;
    oFinancial.mainqtr = currentQuarter;
    oFinancial.mainyear = currentYear;
    oFinancial.qtr = Int32.Parse(currentQuarter
    oFinancial.year = Int32.Parse(currentYear);
    oFinancial.updatedate = DateTime.Now;
}
dc.SaveChanges();

还有一件事:如果 IFID 是主键,那么你应该只要这个对象从 DC来,不是惹它。我相信问题是,你重新设置主键( IFID )的 DC内的另一对象相同的值和EF4乱叫,因为你不能有一个表中的相同的主键值两行。

I am trying to simply update the entity object and I get this error.. All the googling on the error I did takes me to complex explanations... can anyone put it simply?

I am working of of this simple tutorial

http://aspalliance.com/1919_ASPNET_40_and_the_Entity_Framework_4__Part_2_Perform_CRUD_Operations_Using_the_Entity_Framework_4.5

else
                    {
                        //UPDATE
                        int iFid = Int32.Parse(fid.First().fid.ToString());
                        oFinancial.fid = iFid;
                        oFinancial.mainqtr = currentQuarter;
                        oFinancial.mainyear = currentYear;
                        oFinancial.qtr = Int32.Parse(currentQuarter);
                        oFinancial.year = Int32.Parse(currentYear);
                        oFinancial.updatedate = DateTime.Now;
                        // ObjectStateEntry ose = null;
                        // if (!dc.ObjectStateManager.TryGetObjectStateEntry(oFinancial.EntityKey, out ose))
                        // {                      
                        dc.financials.Attach(oFinancial);
                        // }

                        dc.ObjectStateManager.ChangeObjectState(oFinancial, System.Data.EntityState.Modified);
                    }

                    dc.SaveChanges();

here is what is higher up in the code that I use simple to get me the primary key value.. probably a better way but it works.

   var fid = from x in dc.financials
                  where iPhaseID == x.phaseid &&
                         strTaskID == x.ftaskid &&
                         strFundType == x.fundtype &&
                         iCurrentQuarter == x.qtr &&
                         iCurrentYear == x.year
                  select x;

解决方案

If the oFinancial object came from your dc and you never manually detached it, then there is no reason to call the Attach method or to mess with the ObjectStateManager. As long as the dc knows about the object (which it does unless you detach it), then the ObjectStateManager will keep track of any changes you make and update them accordingly when you call dc.SaveChanges().

EDIT: Here's a refactored version of what you posted, hope it helps:

else {
    //UPDATE
    // as long as oFinancial was never detatched after you retrieved
    // it from the "dc", then you don't have to re-attach it.  And
    // you should never need to manipulate the primary key, unless it's
    // not generated by the database, and you don't already have another
    // object in the "dc" with the same primary key value.

    int iFid = Int32.Parse(fid.First().fid.ToString());
    oFinancial.fid = iFid;
    oFinancial.mainqtr = currentQuarter;
    oFinancial.mainyear = currentYear;
    oFinancial.qtr = Int32.Parse(currentQuarter
    oFinancial.year = Int32.Parse(currentYear);
    oFinancial.updatedate = DateTime.Now;
}
dc.SaveChanges();

One other thing: if iFid is the primary key, then you shouldn't mess with it as long as this object came from the dc. I believe the problem is that you're resetting the primary key (iFid) to the same value of another object within the dc, and EF4 is barking because you can't have two rows with the same primary key value in a table.

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

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