0

我正在尝试使用GraphDiff(NuGet 中的最新可用版本)来处理我认为不是非常困难的实体模型。考虑这样的模型:

class A 
{
    public virtual ICollection<B> Bs { get; set; }
}

class B
{
    public virtual ICollection<C> Cs { get; set; }
}

如果我正在更新 A 的实例(称之为aEntity),我可以这样做:

context.UpdateGraph(aEntity, map => 
    map.OwnedCollection(a => a.Bs, withB => 
        withB.OwnedCollection(b => b.Cs)))

现在我有时也想独立更新B。

context.UpdateGraph(bEntity, map => map.OwnedCollection(b => b.Cs));

所以我想我可以通过引入一个属性来“级联”这些更改,如下所示:

class BMapper {
    Expression<Func<IUpdateConfiguration<B>, object>> MappingExpression 
    {
        get 
        {
            return map => map.OwnedCollection(b => b.Cs);
        }
    }
}

...然后在两种情况下都使用它,如下所示:

// Update an A and any of its B's
context.UpdateGraph(aEntity, map =>
    map.OwnedCollection(a => a.Bs, (new BMapper()).MappingExpression))

// Update a B by itself
context.UpdateGraph(bEntity, (new BMapper()).MappingExpression);

单独更新 B 可以正常工作,但更新 A 会在表达式域中失败:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 
    'string' does not contain a definition for 'Body'

有没有办法在 GraphDiff 中共享映射?

4

0 回答 0