locked
Updating by object instance RRS feed

  • Question

  • Hi,

    i want to update an explicit entity (by id) by an object instance.
    For example:

    MyDataContext db = new MyDataContext (connectionString);
    
    Customer customerOrigin = db.Customer.Single(c => c.ID == customer.ID);
    
    // Explicit object instance
    customerOrigin = customer;
    
    // ...instead of:
    /*
    customerOrigin.Name = customer.Name;
    customerOrigin.City = customer.City;
    */
    
    db.SubmitChanges();

     

    I tried the Attach()-Method, but it seems, that this is not the right solution...

    Have you another ideas?

    Thanks a lot!
    Levis

    Monday, December 28, 2009 4:58 PM

Answers

  •  Hi,

        I am afraid , DataContext.Entities is read only.

        So set customerOrigin = customer will take no effect in the entitycollection.

        Since nothing changed in entitycollection , submit change will invoke no operation serverside.

    Best Regards

    Monday, January 4, 2010 3:56 AM
  • Hi,

        Min Hong Tang is absolutely right, since you need to let the object know what were the changes, since the EntityCollection works like an ObservableCollection in terms of objects that where Attached, Removed, or Updates, and by Updated it means that the instance of the Object changed, and this is because the Objects have their state managed using the ObjectSet object, that's why when you go to your GeneratedCode folder and look at the file, there is a list of the EntityCollection object that you have and the permissions that you have on them.

        So in order to achive this you need to make one of two options, that are:

    1. Update the object instead of just passing the new instance, since that won't let the EntitySet know about the changes that were made.
    2. Place a Update method on your WCF RIA Service and just call it with your object, this will also work, or you can use an Invoke instead of the Update, but that depends on you approach to the problem.

       Personally I would suggest the first approach, but this is just my opinion.

     Hope it helps.

    Monday, January 4, 2010 2:40 PM
  • Hi,

       Simply speaking, you can use those commented out line in your thread above.

       customerOrigin.Name = customer.Name;
       customerOrigin.City = customer.City;

    Best Regards

    Tuesday, January 5, 2010 9:58 PM

All replies

  •  Hi,

        I am afraid , DataContext.Entities is read only.

        So set customerOrigin = customer will take no effect in the entitycollection.

        Since nothing changed in entitycollection , submit change will invoke no operation serverside.

    Best Regards

    Monday, January 4, 2010 3:56 AM
  • Hi,

        Min Hong Tang is absolutely right, since you need to let the object know what were the changes, since the EntityCollection works like an ObservableCollection in terms of objects that where Attached, Removed, or Updates, and by Updated it means that the instance of the Object changed, and this is because the Objects have their state managed using the ObjectSet object, that's why when you go to your GeneratedCode folder and look at the file, there is a list of the EntityCollection object that you have and the permissions that you have on them.

        So in order to achive this you need to make one of two options, that are:

    1. Update the object instead of just passing the new instance, since that won't let the EntitySet know about the changes that were made.
    2. Place a Update method on your WCF RIA Service and just call it with your object, this will also work, or you can use an Invoke instead of the Update, but that depends on you approach to the problem.

       Personally I would suggest the first approach, but this is just my opinion.

     Hope it helps.

    Monday, January 4, 2010 2:40 PM
  • Hi,

    thanks for your answers!

    Nuno, can you give me a simple example for option 1? I don't understand, what you mean...

    Tuesday, January 5, 2010 5:58 AM
  • Hi,

       Simply speaking, you can use those commented out line in your thread above.

       customerOrigin.Name = customer.Name;
       customerOrigin.City = customer.City;

    Best Regards

    Tuesday, January 5, 2010 9:58 PM
  • Ah, thanks!

    Thursday, January 7, 2010 5:20 AM