none
How Save Retrieved Entity Object as Different Entity? RRS feed

  • Question

  • Hello,

    I have retrived an existing entity object:

    Customer customer = dataContext.Customer.Where(p => p.CustomerId.CompareTo(customerId) == 0).FirstOrDefault();

    I would like to save this customer as a *new* customer in the database, while retaining the old customer in the database (similar to a "save as" in, say, Word).

    customer.CustomerId = Guid.NewGuid()
    dataContext.TemperatureControllerConfigurationGroups.AddObject(temperatureControllerConfigurationGroup); dataContext.SaveChanges();

    When changes are saved, an error is generated:

    "The property 'TemperatureControllerConfigurationGroupId' is part of the object's key information and cannot be modified."

    Is there a way an existing object can be saved as a new object while retaining the old object in the db? 

    Thanks,
    Chris

     

     

     

     

     

     

     


    Chris
    Friday, August 5, 2011 3:06 PM

Answers

  • Hi Chris;

    First detach the entity from the object context then change the PK of the record then add it back in, see code snippet below.

    Customer customer = 
      dataContext.Customer
            .Where(p => p.CustomerId.CompareTo(customerId) == 0)
            .FirstOrDefault();
    
    // Detach the object so it is not tracked by the ObjectContext. 
    dataContext.Detach(customer);
    
    // Give it a new primary key so that when you add it you don't get
    // duplicate record exception
    customer.CustomerId = Guid.NewGuid()
    // Add the "new record" to the database
    dataContext.TemperatureControllerConfigurationGroups
          .AddObject(temperatureControllerConfigurationGroup); 
    // Save the changes.      
    dataContext.SaveChanges();
    


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, August 6, 2011 5:26 PM
  • your concern i think was focused on dont want to show the empty data in the UI layer. i think you can save the data into cache before you detach entity from context. And then you can savechanges back and show the updated data into context.

    Thanks,

    Werewolf,


    Just a newbie for everything.
    Tuesday, August 9, 2011 6:37 AM
  • Hi Chris;

    Before detaching the Customer object assign the collections to a new variable then once you have done that detach the Customer object and then assign the collections back to the Customer object and then continue with assigning a new CustomerId  and re-attaching the Customer object back to the object context.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, August 9, 2011 5:34 PM

All replies

  • Hi Chris;

    First detach the entity from the object context then change the PK of the record then add it back in, see code snippet below.

    Customer customer = 
      dataContext.Customer
            .Where(p => p.CustomerId.CompareTo(customerId) == 0)
            .FirstOrDefault();
    
    // Detach the object so it is not tracked by the ObjectContext. 
    dataContext.Detach(customer);
    
    // Give it a new primary key so that when you add it you don't get
    // duplicate record exception
    customer.CustomerId = Guid.NewGuid()
    // Add the "new record" to the database
    dataContext.TemperatureControllerConfigurationGroups
          .AddObject(temperatureControllerConfigurationGroup); 
    // Save the changes.      
    dataContext.SaveChanges();
    


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, August 6, 2011 5:26 PM
  • Thank you for the reply.  I tried the code you suggested and it worked but with one problem:

    The customer entity contains a collection of entities (each child entity also contains a collection of entities).  Although these collections were populated prior to detachment, after detachment they are empty.  Is it necessary to manually repopulate these collections again?

    Chris


    Chris
    Monday, August 8, 2011 7:13 PM
  • your concern i think was focused on dont want to show the empty data in the UI layer. i think you can save the data into cache before you detach entity from context. And then you can savechanges back and show the updated data into context.

    Thanks,

    Werewolf,


    Just a newbie for everything.
    Tuesday, August 9, 2011 6:37 AM
  • Hi Chris;

    Before detaching the Customer object assign the collections to a new variable then once you have done that detach the Customer object and then assign the collections back to the Customer object and then continue with assigning a new CustomerId  and re-attaching the Customer object back to the object context.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, August 9, 2011 5:34 PM