views:

27

answers:

1

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;
+1  A: 

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.

Scott Anderson
Yes fid is the primary key.. But I need to get EF the primary key of the row in order for it to update it I would guess.. ill look into it tomorrow
punkouter
yup. that that seems to fix it.. I was putting in stuff I didnt even understand... maybe there is a more elegant way of getting the primary key though?
punkouter
You should already know what it is, if you have an oFinancial record. You can query the dc for that object and then update any properties, then call SaveChanges() and everything happens automagically :)
Scott Anderson
I see what you are saying maybe int iFid = Int32.Parse(fid.First().fid.ToString()); is not the best way to get the primary key but I need to do that since I am manual building up this object.. I am not getting a existing one .. since I might INSERT .. and in that case I have no primary key.. see above edit
punkouter