Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Replace an Entity in Entity Framework Context

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
24 Feb 2010CPOL 16.6K   1   1
I was struggling recently with a problem I KNEW had to be solvable in a much easier way than replacing ALL the fields in my managed entity by hand and then saving it. I wanted a way that I could simply replace the managed entity with a new one that had been created from an XML document.Turns...
I was struggling recently with a problem I KNEW had to be solvable in a much easier way than replacing ALL the fields in my managed entity by hand and then saving it. I wanted a way that I could simply replace the managed entity with a new one that had been created from an XML document.

Turns out it wasn't impossible and my hat goes off to the EF once again.

Basically I had to loop through the XML document creating entities from it and then compare to the DB to see if they already existed. If they did, I had to update them, else I had to add them. A simple query let me know if it existed, but the updating code is where the magic lies:

C#
var oldItem = (from c in entitiesContext.table
               where c.EntityUID == newItem.EntityUID
               select c).FirstOrDefault();

if (oldItem == null)
{
    entitiesContext.table.AddObject(newItem);
}
else
{
    entitiesContext.Detach(oldItem);
    entitiesContext.AttachTo("table", newItem);
    entitiesContext.ObjectStateManager.GetObjectStateEntry(newItem).ChangeState(System.Data.EntityState.Modified);
    entitiesContext.SaveChanges();
}



Voila! Entity replaced and saved from the generated entities.

Now I am by no means a EF guru, but this little trick let me save a TON of time on plumbing as my entities have a lot of fields that need replacing.

WARNING: Be sure the generated entities are accurate, after all, you're removing those in the data and replacing them with the generated ones!

License

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


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionA small update Pin
Jan Kratochvíl13-Oct-13 3:28
Jan Kratochvíl13-Oct-13 3:28 
Hi, solving a similar problem, I think the code can be updated to be more readable (maybe the APIs I use were not available at the time).

In the else, block, it would be nicer to write the following:
C#
((IObjectContextAdapter)entitiesContext).ObjectContext.Detach(oldItem);
entitiesContext.Series.Attach(newItem);
entitiesContext.Entry(newItem).State = EntityState.Modified;


The cast is needed only when you use the DbContext instead of the ObjectContext as your context base class.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.