Click here to Skip to main content
15,909,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm novice to EF 3.5. I'm trying to move data from main table to history table. If I have 4 rows in the main table i need to move those 4 rows to History table, then I need to update the main table. That is my requirement.

But I'm not able to move the data from main to history table. While processing (inserting) the 2nd record into history table I'm getting the following error message.

An object with the same key already exists in the ObjectStateManager. The existing object is in the Unchanged state. An object can only be added to the ObjectStateManager again if it is in the added state.


How to change the state of the Entity from Modified/Unchanged to Added entity state in Entity frame work 3.5. Not using EF 4.

Here is my code snippet.


public string UpdateAllAgreementDealers(REFIEntities context, long agmtId, DateTime expirationDate, string userId)
{
IQueryable<agmt_delr> agmtDelr;
REFI.DAL.EntityFW.AGMT_DELR agmtDlr;
REFI.DAL.EntityFW.AGMT_DELR_HIST delrHistory = new AGMT_DELR_HIST();
string delrHistoryValue = "TRUE";
System.Data.EntityKey ent;
try
{
agmtDelr = (from item in context.AGMT_DELRS
where item.AGMT.AGMT_ID_REFI == agmtId
select item);
// To update all the agreement dealers taken a loop
foreach (var item in agmtDelr)
{
if (item.DELR_CONTR_EXPI_DT > expirationDate)
{
agmtDlr = GetAgmtDealer(item.AGMT_DELR_ID);
if (agmtDlr != null)
{
//moving main to history table.
delrHistory.DELR_ID = agmtDlr.DELR.DELR_ID;
delrHistory.AGMT_DELR_ID = agmtDlr.AGMT_DELR_ID;
delrHistory.CONTR_TYP = agmtDlr.CONTR_TYP;
delrHistory.DELR_CONTR_EFF_DT = agmtDlr.DELR_CONTR_EFF_DT;
delrHistory.DELR_CONTR_EXPI_DT = agmtDlr.DELR_CONTR_EXPI_DT;
delrHistory.IS_CUR = agmtDlr.IS_CUR;
delrHistory.AGMT = agmtDlr.AGMT;
delrHistory.MOD_BY = userId;
delrHistory.MOD_DT = System.DateTime.Now;
delrHistory.CRTE_BY = userId;
delrHistory.CRTE_DT = System.DateTime.Now;

ObjectStateEntry objEn;
DbEntity.ObjectStateManager.TryGetObjectStateEntry(delrHistory, out objEn);
if (objEn.State == System.Data.EntityState.Modified)
{
//objEn.State = System.Data.EntityState.Added;

//I'm not able to uncomment the above line as it saying read only property. But I need the Entity state should be added to add a new item.



DbEntity.Detach(delrHistory);
DbEntity.CreateEntityKey("AGMT_DELR_HISTS", delrHistory);
DbEntity.Attach(delrHistory);
DbEntity.SaveChanges();

}

DbEntity.AddToAGMT_DELR_HISTS(delrhistory);
// here I'm getting the error message while adding 2nd record.
//An object with the same key already exists in the ObjectStateManager. The existing object is in the Unchanged state. An object can only be added to the ObjectStateManager again if it is in the added state.

DbEntity.SaveChanges();
}
item.DELR_CONTR_EXPI_DT = expirationDate;
item.IS_CUR = true;
item.MOD_BY = userId;
item.MOD_DT = System.DateTime.Now;

}
}


if (agmtDelr != null)
return UpdateDealers();
else
return null;
}
catch (Exception e)
{
return null;
}
finally
{
agmtDelr = null;
}
}

Please suggest me the approach
Posted
Updated 23-Jan-12 20:11pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900