Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6340895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:55:17+00:00 2026-05-24T19:55:17+00:00

I use Entity Framework to deal with my database, and as I googled a

  • 0

I use Entity Framework to deal with my database, and as I googled a lot, I ran into some solution around updating my entities, here is extension method for updating entity the code :

public static void AttachUpdated(this ObjectContext context, EntityObject objectDetached)
{
   if (objectDetached.EntityState == EntityState.Detached)
   {
      object currentEntityInDb = null;

      if (context.TryGetObjectByKey(objectDetached.EntityKey, out currentEntityInDb))
      {
         context.ApplyPropertyChanges(objectDetached.EntityKey.EntitySetName, objectDetached);

         //(CDLTLL)Apply property changes to all referenced entities in context
         //Custom extensor method
         context.ApplyReferencePropertyChanges((IEntityWithRelationships)objectDetached,
                                               (IEntityWithRelationships)currentEntityInDb); 
       }
       else
       {
           throw new ObjectNotFoundException();
       }
    }
}

public static void ApplyReferencePropertyChanges(this ObjectContext context,
                                                 IEntityWithRelationships newEntity,
                                                 IEntityWithRelationships oldEntity)
{
   foreach (var relatedEnd in oldEntity.RelationshipManager.GetAllRelatedEnds())
   {
      var oldRef = relatedEnd as EntityReference;

      if (oldRef != null)
      {
         // this related end is a reference not a collection
         var newRef = newEntity.RelationshipManager.GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference;

         oldRef.EntityKey = newRef.EntityKey;
      }
   }
}

And here is the code for saving or adding new object :

public void save (Category obj)
{
    OurWebSiteEntities en = new OurWebSiteEntities();

    if (obj.CategoryID == -1)
    {
       en.AddToCategories(obj);
    }
    else
       en.AttachUpdated(obj);

    en.SaveChanges();
}

And finally this the code I use to update my category entity in asp.net mvc :

if (cat.Category.CategoryID == -1)
{
   // it is a new category
   // and needs some property initialization   
}
else
   catItem = _categoryRepo.Items.FirstOrDefault(x => x.CategoryID == cat.Category.CategoryID);

TryUpdateModel(catItem, "Category");

_categoryRepo.Save(catItem);

But unfortunately it does not work for updating any category, and I am only able to create new one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:55:18+00:00Added an answer on May 24, 2026 at 7:55 pm

    In your repository, you could create an update function and set entity state to modified.

    public abstract class Repository<T>
    {    
       public void Update(T entity) {
            Context.Entry(entity).State = EntityState.Modified;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use Microsoft Entity Framework with Oracle database?
How to use transactions in Entity Framework? I read some links on Stackoverflow :
I'm trying to use Entity Framework and it put it's connection string into app.config.
I use entity Framework 4.2 and MVC 4 I Got this model/Database structure UserInformation
i'm using MS northwind database, and use Entity Framework. I want to create new
Somebody advised me to use Entity Framework here when I asked this question: what
I'm trying to use Entity Framework to query database and I have following code
I'm trying to use Entity Framework for my model/data access, and running into speed
I have silverlight application, where I use Entity Framework(PostgreSQL) and WCF(not RIA). Here is
I am trying here to use Entity Framework with Stored procedures and POCOS and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.