[英]Conditional mapping with graphdiff
I have following entities in my DbContext
: 我的
DbContext
有以下实体:
public class A
{
public A()
{
Bs = new List<B>();
}
public ICollection<B> Bs { set; get; }
}
Sometimes I Want to update a
graph: 有时候我想更新
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);
I want to update a
with its Bs
(update C
, D
, E
too) using graphdiff
: 我想使用
graphdiff
使用其Bs
来更新a
(也更新C
, D
, E
):
dbContext.UpdateGraph(a,map=>map.OwnedCollection(x=>x.Bs));
This updates A
, B
s, C
s, D
s, but not E
s. 这将更新
A
, B
, C
, D
,而不更新E
So I think, I need to define a conditional mapping for graphdiff
, to update E
s too, somethings like: 所以我认为,我需要为
graphdiff
定义条件映射,也要更新E
,例如:
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? 有什么办法可以做这项工作?
You can use this with graphdiff: 您可以将其与graphdiff一起使用:
dbContext.UpdateGraph(a, map => map
.OwnedCollection(b => p.Bs, with => with
.AssociatedCollection(p => p.Es)));
see this link: GraphDiff Explanation 看到这个链接: GraphDiff说明
I don't believe that is possible using your current class structure. 我认为使用您当前的类结构是不可能的。 However, I found a way to accomplish this, but you have to make some changes in your code.
但是,我找到了一种方法来实现此目的,但是您必须在代码中进行一些更改。
Update the A
: 更新
A
:
public class A
{
public A()
{
Cs = new List<C>();
Ds = new List<D>();
}
//PK
public int AId { get; set; }
public ICollection<C> Cs { set; get; }
public ICollection<D> Ds { set; get; }
}
Update B
, C
, and D
: 更新
B
, C
和D
:
public class B
{
public int BId { get; set; }
}
public class C : B
{
//FK that links C to A
public int FK_C_AId { get; set; }
}
public class D : B
{
//FK that links D to A
public int FK_D_AId { get; set; }
public ICollection<E> Es { get; set; }
public D()
{
Es = new List<E>();
}
}
In order to maintain the TPH strategy, some mappings are necessary. 为了维持TPH策略,一些映射是必要的。
modelBuilder.Entity<A>()
.HasMany(i => i.Cs)
.WithRequired()
.HasForeignKey(i => i.FK_C_AId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<A>()
.HasMany(i => i.Ds)
.WithRequired()
.HasForeignKey(i => i.FK_D_AId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<B>()
.Map<C>(m => m.Requires("Discriminator").HasValue("C"))
.Map<D>(m => m.Requires("Discriminator").HasValue("D"));
Now, you have almost the same database structure. 现在,您具有几乎相同的数据库结构。
C
and D
are still mapped to the same table. C
和D
仍映射到同一表。
Finally, update the Graph: 最后,更新图:
context.UpdateGraph(a, map => map
.OwnedCollection(b => b.Cs)
.OwnedCollection(b => b.Ds, with => with
.AssociatedCollection(e => e.Es)));
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.