I receive a lot of random emails from developers with Entity Framework questions. (This is not a request for more! :)) If I’m too busy to even look or if it’s something I can’t answer off the top of my head, I swallow my pride and ask the person to try the MSDN forums. If the email is from a complete stranger and has gobs and gobs of code that email will surely get a "please try MSDN forums" reply.
But sometimes I’m not in my usual state of “too much to do” panic and get a question that is short & sweet and I can answer it effortlessly. This is one of those types of questions.
Question from Arda Çetinkaya
Hi;
I am contacting with you with a suggestion of my friend who is one of the best MVPs of Turkey…So I have a small question if you can answer it I will be very happy…
What I am trying to do is updating a poco entity with entity framework…My poco entity is not in context,so I am attaching it first…But no change is done…How can I update my poco entities…If you have any resource for it,I really need it…
Reply
You would have the same problem with EntityObjects as with POCOs. If you are using EF4, then you have a lot of options that you did n ot have in EF1.
Assuming it’s EF4, the simplest is to use the new ObjectContext.ObjectStateManager.ChangeState (might be ChangeObjectState) method. But that means you need to know what the state should be …added, updated or deleted.
That’s just one way and might be just what you need or might not fit your scenario.
If you search for changestate/changeobjectstate and Entity Framework on the web you should find lots of blog posts and articles on this.
Good Luck
julie
Follow-up from Arda 20 minutes later
Thanks a lot for your reply…It helped me to clear it out…
With the following code change, I got it…Thanks a lot…You saved my life (:
public static void UpdateConfigurationSetting(ConfigurationSetting configurationSettingToUpdate)
{
try
{
using (DBEntities entities = new DBEntities ())
{
entities.ConfigurationSetting.Attach(configurationSettingToUpdate);
ObjectStateEntry entry = entities.ObjectStateManager
.GetObjectStateEntry(configurationSettingToUpdate);
entry.ObjectStateManager
.ChangeObjectState(configurationSettingToUpdate,
System.Data.EntityState.Modified);
entities.SaveChanges();
}
}
}