Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. LINQ
  4. updating in entity framework

updating in entity framework

Scheduled Pinned Locked Moved LINQ
helpcsharpasp-net
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ahmed Galal
    wrote on last edited by
    #1

    in my asp.net edit page, i load an entity and its childs from my context and save it in the Application object to preserve it during page postbacks, doing this will make my entity lose its context so i have to reattach it when the user finally click edit. the problem is when i reattach and call SaveChanges and ApplyPropertyChanges only the main entity get updated and not the childs. i have tried to attach the child entities too using AddObject or Attach but i always get exceptions like "An object with a temporary EntityKey value cannot be attached to an object context." OR "An entity object cannot be referenced by multiple instances of IEntityChangeTracker." my code look like this:

            var originalEntity = (from x in verifiersDB.VerificationHosts where x.ID == ID select x).FirstOrDefault();
    
            originalEntity.VerificationHostSearchs.Load();
            verifiersDB.Attach(originalEntity);
    
            foreach (var l in verificationHost.VerificationHostSearchs)
                verifiersDB.AddObject("VerificationHostSearchs",l);  //calling AddObject or Attach will throw an exception
            
            verificationHost.Type = short.Parse(ddlHostType.SelectedValue);
            verificationHost.HostName = txtHostName.Text;
            verificationHost.Description = txtHostDescription.Text;
            verificationHost.HostUrl = txtHostURL.Text;
    
            verifiersDB.ApplyPropertyChanges("VerificationHosts", verificationHost);
    
            verifiersDB.SaveChanges();
    

    verificationHost is retrieved from the Application state Any help will be appreciated. Thanks in advance.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups