This is a migrated thread and some comments may be shown as answers.

AttachCopy not populating navigation properties/references

3 Answers 46 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Martin
Top achievements
Rank 1
Martin asked on 28 Feb 2013, 04:45 PM
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

3 Answers, 1 is accepted

Sort by
0
Yordan
Telerik team
answered on 05 Mar 2013, 12:18 PM
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.
0
Martin
Top achievements
Rank 1
answered on 05 Mar 2013, 01:47 PM
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
0
Damyan Bogoev
Telerik team
answered on 08 Mar 2013, 12:03 PM
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.
Tags
Development (API, general questions)
Asked by
Martin
Top achievements
Rank 1
Answers by
Yordan
Telerik team
Martin
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or