使用graphdiff进行条件映射 [英] Conditional mapping with graphdiff

查看:110
本文介绍了使用graphdiff进行条件映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 DbContext 中有以下实体:

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

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

有时我想更新 a 图:

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);

我想用< a 更新code> Bs (更新 C D E ),也使用 graphdiff

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

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

此更新 A B s, C s, D s,但不包含 E s。

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

所以我想,我需要为 graphdiff ,也要更新 E ,例如:

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?

推荐答案

您可以将其与graphdiff一起使用:

You can use this with graphdiff:

dbContext.UpdateGraph(a, map => map
    .OwnedCollection(b => p.Bs, with => with
    .AssociatedCollection(p => p.Es)));

请参阅此链接:
GraphDiff解释

这篇关于使用graphdiff进行条件映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆