The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Detach Attach issue

Last post 04-04-2008, 6:53 AM by normcmi. 5 replies.
Sort Posts: Previous Next
  •  01-17-2008, 11:44 PM 7668

    Detach Attach issue

    This should work shouldnt it?

    Code:
    XpVehicleVersionCollection versionColl = new XpVehicleVersionCollection();
                versionColl.Query.Where(versionColl.Query.BrandCode == "ALFA");
                versionColl.Query.Load();
    
                XpVehicleVersion first = versionColl[0];
                XpVehicleVersion second = versionColl[1];
    
                //get a car from the first VehicleColl
                XpVehicle car = first.XpVehicleCollectionByXpVehicleVersionID[0];
    
                //detach it
                first.XpVehicleCollectionByXpVehicleVersionID.DetachEntity(car);
    
                //and attach it to the second.
                second.XpVehicleCollectionByXpVehicleVersionID.AttachEntity(car);
    
                versionColl.Save();
     
    I can see the VehicleVersionID change to the one I attach to in 'car'.
    But when i do the versionColl.Save() the id get changed back to its originail.
  •  01-18-2008, 3:41 AM 7672 in reply to 7668

    Re: Detach Attach issue

    No, attach/detach just moves an entity from one in memory collection to another. It does not understand about foreign key columns. It is up to you to ensure that the car actually belongs to the new collection by setting its foreign key in your code. That is what flags a row as dirty, the column as modified, and triggers action during Save.

    David Neal Parsons
    www.entityspaces.net
  •  01-18-2008, 3:56 AM 7673 in reply to 7672

    Re: Detach Attach issue

    Ok, so this would do the trick than??

    Code:
     //detach it
    first.XpVehicleCollectionByXpVehicleVersionID.DetachEntity(car);
          
    
     //and attach it to the second.
    second.XpVehicleCollectionByXpVehicleVersionID.AttachEntity(car);
    car.XpVehicleVersionID = first.XpVehicleVersionID.Value;
    
    versionColl.Save();
  •  01-18-2008, 6:04 AM 7682 in reply to 7673

    Re: Detach Attach issue

    I think you mean

    car.XpVehicleVersionID = second.XpVehicleVersionID;

     


    David Neal Parsons
    www.entityspaces.net
  •  01-18-2008, 6:29 AM 7684 in reply to 7682

    Re: Detach Attach issue

    Oeps, indeed.Embarrassed

     Thanks for the hint do.

     

  •  04-04-2008, 6:53 AM 8705 in reply to 7684

    Re: Detach Attach issue

    There is a bug in entity spaces concerning the modified columns in fk references. The AttachEntity sets the FK value, but does not update the modified column collection. So when the save is called, an Object Reference error fires. The work around we use it to set the car.XpVehicleVersionID value before attaching the entity. This causes the car entity to be marked modified and adds an entry to modfied columns.
    Code:
    //detach it
    first.XpVehicleCollectionByXpVehicleVersionID.DetachEntity(car);
          
    
     //and attach it to the second.
    car.XpVehicleVersionID = second.XpVehicleVersionID.Value;
    second.XpVehicleCollectionByXpVehicleVersionID.AttachEntity(car);
    
    versionColl.Save();
     Norm
View as RSS news feed in XML