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

  • Home
  • SEARCH
  • 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 124979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:05:10+00:00 2026-05-11T05:05:10+00:00

Ok, I am attempting to use this custom extension to perform an entity update.

  • 0

Ok, I am attempting to use this custom extension to perform an entity update. Right now it should update all properties and then all of its related ends (references). The problem is even though the entity with changes does have a related end with an entity key that is valid and correct (verified in the debugger its there). When the RelationshipManager gets it they key is null.. Anyone see anything wrong with this code?

public static void ApplyChanges(this ObjectContext context, EntityObject entityWithChanges)     {         if (entityWithChanges.EntityState == EntityState.Detached)         {             object entityCurrentlyInDB = null;              if (context.TryGetObjectByKey(entityWithChanges.EntityKey, out entityCurrentlyInDB))             {                 context.ApplyPropertyChanges(entityWithChanges.EntityKey.EntitySetName, entityWithChanges);                  foreach (var relatedEnd in ((IEntityWithRelationships)entityCurrentlyInDB).RelationshipManager.GetAllRelatedEnds())                 {                     var oldRef = relatedEnd as EntityReference;                      if (oldRef != null)                     {                         var newRef =                             ((IEntityWithRelationships)entityWithChanges).RelationshipManager                             .GetRelatedEnd(oldRef.RelationshipName, oldRef.TargetRoleName) as EntityReference;                         oldRef.EntityKey = newRef.EntityKey;                     }                 }             }             else             {                 throw new ObjectNotFoundException();             }          }     } 

This is just a modified version of what I found Here

Appreciate the help.

UPDATE: This is the Update Method

public static void UpdateTemplate(Template template)     {         using (TheEntities context = new TheEntities())         {             context.ApplyChanges(template);             try             {                 context.SaveChanges();             }             catch (OptimisticConcurrencyException)             {                 context.Refresh(RefreshMode.ClientWins, template);                 context.SaveChanges();             }              context.RemoveTracking(template);         }     } 

This is the unit test:

[TestMethod]     public void CanUpdateATemplate()     {         Template template = new Template();         template.Name = 'Test';         template.Description = 'Test';          TemplateType type = TemplateManager.FindTemplateTypeByName('Test');          if (type == null)         {             type = new TemplateType();             type.Name = 'Test';         }          template.TemplateType = type;          TemplateManager.AddTemplate(template);          template = TemplateManager.FindTemplateByID(template.TemplateID);          Assert.IsNotNull(template);          type = TemplateManager.FindTemplateTypeByName('Round');          if (type == null)         {             type = new TemplateType();             type.Name = 'Round';         }          template.TemplateType = type;          TemplateManager.UpdateTemplate(template);          template = TemplateManager.FindTemplateByID(template.TemplateID);          Assert.IsNotNull(template);         Assert.IsTrue(template.TemplateType.Name == 'Round');          TemplateManager.DeleteTemplate(template);          template = TemplateManager.FindTemplateByID(template.TemplateID);          Assert.IsNull(template);     } 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T05:05:11+00:00Added an answer on May 11, 2026 at 5:05 am

    This is simply not possible due to the way Detach works in EF.

    I found that if I added key information myself I could get the generic call to work and even save it.

    The problem is that the moment you go to return an entity after calling Detach on it you lose all relationship data. Ive found some write ups on writing graph managers for each entity but I find that a waste seeing as EF should do this stuff (supposedly it will in v2).

    EF simply is not ready for N-Tier deployments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 127k
  • Answers 127k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This may be an issue of character-encoding. You can try… May 12, 2026 at 5:34 am
  • Editorial Team
    Editorial Team added an answer The problem here isn't converting an extension method - it's… May 12, 2026 at 5:34 am
  • Editorial Team
    Editorial Team added an answer First, you could change the settings in VS2008 to break… May 12, 2026 at 5:34 am

Related Questions

OK I would like to know this once and for all, how can I
Alright, I am going to state up front that this question may be too
I have to write code to clone a database entry with associated data in
I am using SQLite3 for iPhone development and I'm attempting to wrap a few

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.