GraphDiff icon indicating copy to clipboard operation
GraphDiff copied to clipboard

Conditional mapping with graphdiff

Open msanaei opened this issue 9 years ago • 2 comments

I have following entities in my DbContext:

enter image description here

public class A
{
   public A()
   {
       Bs = new List<B>(); 
   }

   public ICollection<B> Bs { set; get; }
}   

Sometimes I Want to update a graph:

var a = dbContext.As
       .AsNoTracking()
       .Include(x=>x.Bs)
       .firstOrDefault();

var c = new C();
a.Bs.Add(c);

var d = new D();
var e1 = new E();
var e2 = new E();
d.Es.Add(e1); //<-- added new E
d.Es.Add(e2); //<-- added new E

a.Bs.Add(d);

I want to update a with its Bs(update C,D,E too) using graphdiff:

dbContext.UpdateGraph(a,map=>map.OwnedCollection(x=>x.Bs));

It updates A, Bs, Cs, Ds, but not Es.

So I think, I need to define a conditional mapping for graphdiff, to update Es too, somethings like:

dbContext.UpdateGraph(a,map=>map.OwnedCollection(x=>x.Bs.OfType<D>(), 
                                             with =>with.OwnedCollection(t=>t.Es))
                                .OwnedCollection(x=>x.Bs.OfType<C>()));

Is there any way to do this job?

msanaei avatar Sep 18 '15 15:09 msanaei

Unless I'm mistaken about what you're asking, you can do this by using 2 maps, one with the sometimes child Entities and one without.

b9chris avatar Mar 17 '16 14:03 b9chris

Could you write this code please?

msanaei avatar Mar 20 '16 14:03 msanaei