How can I prevent GraphDiff from removing missing children in the object graph?

289 Views Asked by At

I'm currently working on a project that requires we be able to pull up the exact state of an object and associated children at an point in time. To do this we've come up with a model similar to this:

public class Parent
{
    [Key, Column(Order = 0)]
    public int Id { get; set; }

    [Key, Column(Order = 1)]
    public int Version { get; set; }

    // Additional properties & version management
}


public class Child
{
    [Key, Column(Order = 0)]
    public int Id { get; set; }

    [Key, Column(Order = 1)]
    public int Version { get; set; }

    [ForeignKey, Column(Order = 2)]
    public int ParentId { get; set;

    public virtual Parent Parent { get; set; }

    [ForeignKey, Column(Order = 3)]
    public int ParentVersion { get; set; }

    // Additional properties & version management
}

Lets say you have an object:

Parent 1v1
    Child 1v1
    Child 2v1

This will save with GraphDiff as expected. Then later, we load parent 1v1, and change something about it. The version will bump to 2, and the child object versions will bump to 2. So now this object:

Parent 1v2
    Child 1v2
    Child 2v2

will also save like we want. A new record is inserted for each of these objects, and the old versions remain. However, if we change the child 1 object only, so we go from the above model to:

Parent 1v2
    Child 1v3   // We changed something here
    Child 2v2

and use GraphDiff to save, it sees that Child 1v2 is no longer in the graph, and deletes it from the database. The only way I see around this is to have to 2 get methods. One that loads what's actually usable, the max version parent and children, and one that loads the entire graph including every historical object.

I'd prefer not to do that. This Blog Post announcing new GraphDiff functionality seems to indicate that there may be some auditing capabilities available, but I cannot find any documentation on how to use this.

Is there a way to use GraphDiff in an Add/Update only mode?

0

There are 0 best solutions below