Disconnected Complex Graph With Disconnected Child Entities

entity-framework-6 graphdiff


I have several child items in a complicated graph. The parent-child relationship is a one-to-many relationship with an independent association. Since I wish to avoid manual synchronization, I am aware that I cannot persist my graph without it. I started investigating the GraphDiff approach after finding it to be promising, but because of my particular scenario, I was unable to do what I had hoped to. This problem is solely connected to GraphDiff, although I'm open to other fixes as well.

public class person
    public virtual int Id {get; set;} //primary key
    public virtual string Name {get; set;}
    public virtual ICollection<Car> Cars {get; set;}
public class Car
    public virtual int PersonId {get; set;} // is a foreign key 
    public virtual Datetime BoughtDate {get; set;}   // this property and above mentioned foreign key together makes a primary key in database

    public virtual DateTime InsuraceStartDate {get; set;}
    public virtual DateTime InsuraceExpiryDate {get; set;}
    public virtual DateTime LastServiceDate {get; set;}

My collection of People.Cars may contain one new car object at any given moment. 2. An existing object for a car that has new values. (Service or insurance dates)

Id property for both people and cars will be 0.

    Person _person = GetPersonToAddOrUpDate();
     int id =   _person.id; //id will be 0.

We are unsure whether the object we got needs to be altered or added at this time. Cars' child entities' PersonId will also be 0. If we call now.

     Context.UpdateGraph(_person, m => m.OwnedCollection(x => x.Cars)); 
  throws exception at FindEntityMatching(entity). 

I need to edit the person's Id property in order to fix this.

     var dbPersonId  = Context.Single<Person>(x => x.Name == _person.Name).Id;
    _person.id = dbPersonId != null ? dbPersonId : 0;
     Context.UpdateGraph(_person, m => m.OwnedCollection(x => x.Cars));

It eliminates every entity in dbPerson. cars and _person is added. vehicle to dbPerson saving and cars. The database's Cars table and the _person table's records are both removed, according to this statement. There are cars added. I became aware of this since the child entities currently lack an Id property. I have to manually set them.

  foreach(var car in _person.Cars)
        car.PersonId = _person.Id

Then, if you call

   Context.UpdateGraph(_person, m => m.OwnedCollection(x => x.Cars));  

This will update the object matching in the persistent object, add entities in the collection from transitory objects lacking in the persistent object, and delete the remaining entities from the persistent object.

     var dbCars = conext.Where(x => x.Personid == _person.Id).Select(x).ToList();

say db Cars. There are 8 and people. Cars. There are 3. Once you run, (one new car and two used cars with updated values).

    Context.UpdateGraph(_person, m => m.OwnedCollection(x => x.Cars));

2 vintage cars owned by _person. Updates will be made to any matching cars in dbCars. one new vehicle from _person. Vehicles that don't match in dbCars will be added.

Since we introduced a new car, the number of dbCars must now be 9. Check the count now.

  var dbCarCount = conext.Where(x => x.Personid == _person.Id).Select(x).ToList().Count();

dbCarCount shall equal 3. The remaining 6 vehicles from dbCars are being eliminated.

I hope I'm mistaken because I prefer using your solution. I'm at a standstill with this project. If there is anything I'm overlooking or you require more details, do let me know. I am aware that my writing is unclear because I wanted to include all of my GraphDiff testing experience. I hope you take up this matter.

11/15/2013 8:28:56 PM

Popular Answer

There are three things it should perform if you have an unconnected graph that you want to combine with the database:

  1. Add fresh entities
  2. Refresh existing entities
  3. Removing entities from the graph

You seem to be experiencing trouble with the final section.

You are not utilizing GraphDiff as it was intended if the graph has not already been obtained from the database. You will have to add and update information manually.

11/19/2013 4:01:09 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow