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 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

Related Questions

I am attempting to use MapKit to create a custom map (similar to this
I am attempting to implement a custom view. This view should display an image
Attempting to use the data series from this example no longer passes the JSONLint
I'm attempting to use code I've found that uses Win32. However, I'm getting this
I am attempting to use livequery. I unfortunately am stuck using jquery 1.2.6. This
I am attempting to compress a string via use of zlib (I've tried this
Im attempting to use a C++ extension for Python called PySndObj. and getting an
I am attempting to use a custom font for a TextView on Android, following
I'm attempting to use Redmon http://www.winimage.com/misc/redmon/ to send print jobs to a custom C#
Attempting to use a custom hex color for my css triangle (border). However since

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.