AttachCopy not populating navigation properties/references

4 posts, 0 answers
  1. Martin
    Martin avatar
    2 posts
    Member since:
    Oct 2012

    Posted 28 Feb 2013 Link to this post

    Hi,

    I am trying to attach an object  to the ORM context using AttachCopy ( the object exists in the database already, but was loaded via a different mechanism, I simply want to save changes).  The AttachCopy works and I can SaveChanges no problem, however when I call AttachCopy, the navigation properties are not populated.  If I create a new object and set the ids for the nav properties then add it to the context using 'Add'  then all the nav properties are populated.

    Is this expected?  Is there a way to have all navigation properties populated before calling SaveChanges?

    I can send my .rlinq file if it would help.

    Regards,

    Martin
  2. Yordan
    Admin
    Yordan avatar
    39 posts

    Posted 05 Mar 2013 Link to this post

    Hello Martin,

    If I understood you correctly you are trying to set the navigation property of an object and after that you call 
    AttachCopy method, expecting the navigation property to be populated. I made and tested a short snippet that is working fine on my side:

    using (EntitiesModel context = new EntitiesModel())
    {
        using (EntitiesModel context2 = new EntitiesModel())
        {
            Car car = context.Cars.Last();
     
            // Person is navigation property and PersonGuid is the foreign key for Car
            car.Person = null;
     
            car.Name = "new BMW";
     
            car.PersonGuid = Guid.Parse("b1ee38c7-a384-e211-93b4-cc52af499060");
     
            // Here  car.Person is populated (is not null)
     
            Car attachedCar = context2.AttachCopy<Car>(car);
     
            // Here  car.Person and attachedCar.Person are populated (are not null)
        }
    }

    Could you please test this setup and verify that it works on your end? In case it does, can you specify the differences in your scenario? If you send some sample code it would be very helpful.

    I am looking forward to your feedback.

    Kind regards,
    Yordan
    the Telerik team
    OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Sign up for a free webinar to see all the new stuff in action.
  3. DevCraft banner
  4. Martin
    Martin avatar
    2 posts
    Member since:
    Oct 2012

    Posted 05 Mar 2013 Link to this post

    Hi Yorden,

    Your scenario is not quite what I meant, what I want is as follows:

    Car detachedCar = <car object from a message queue>;
     
    //at this point the detachedCar object has PersionGuid set to a GUID of an existing Person GUID and detachedCar.Person object = null
      
    using (EntityContext context  = new EntityContext())
    {
                context .AttachCopy(detachedCar);
     
    //I want detachedCar.Person object to be populated now so I can do some calulations on the full object graph
     
                context .SaveChanges();
    }

    If I am creating a new Car object then it works as follows:
    Car car = new Car();
    car.PersonGuid = <GUID of an existing person>;
     
    //at this point car.PersionGuid is set and car.Person object = null
     
    using (EntityContext context  = new EntityContext())
    {
                context .Add(car);
     
    //At this point the car.Person object is populated with the Person object corresponding to the <GUID of existing person> from the DB.
     
                context .SaveChanges();
    }

    Any ideas?

    Cheers,

    Martin
  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 08 Mar 2013 Link to this post

    Hi Martin,

    You need mark the association as master using the navigation member configuration method IsManaged() in order to handle this scenario.

    Hope that helps.

    Regards,
    Damyan Bogoev
    the Telerik team
    OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Check out all of the latest highlights.
Back to Top