NHibernate fluent 如何防止子项被更新

3
我所使用的应用程序使用本地化。它的构建方式是检查实体(向下遍历结构)并翻译标记为“translate”的每个属性。
这些翻译存储在单独的翻译表中。
这一切都很好,但它让我面临一个问题,即当我使用翻译更新实体时,现在我的“默认”值中得到了翻译后的值。而我不想要那样。
让我尝试更好地解释一下。
数据库:enter image description here footprintlinevLue的映射:
public class FootprintLineValueMap : ClassMap<FootprintLineValue> {
 public FootprintLineValueMap() {
    Table("FootprintLineValue");

    Id(x => x.Id).GeneratedBy.Identity().Column("Id");
    References(x => x.FootprintLine).Column("FootprintLineId");
    References(x => x.CategoryValue).Column("CategoryValueId").Cascade.None();

  }

正如您所看到的,footprintline有多个值引用categoryValue。 categoryvalue是本地化的。

当我现在检索footprintlines时,我们的框架将通过我们的translationservice并自动翻译相应文化的CategoryValue的名称和描述。如果在CategoryValueLocal中找不到翻译,则会使用CategoryValue中的默认值。

然而...如果我保存Footprintline,它将把翻译后的值保存回CategoryValue(覆盖默认值),而不是忽略它。

CategoryValues不是值对象,可能会被更改,因此我无法将它们设为只读。我尝试将引用映射为Cascade.None,但似乎没有任何作用。

我希望能够以简单的方式将其标记在映射中,以便我们可以继续使用我们的TranslationService,而不必找出其他处理本地化的方法。

1个回答

2

将属性标记为不可更新。

Map(x => x.Description).Not.Update();

你甚至可以定义一个约定来这样做。
class TranslatedPropertiesConvention : AttributePropertyConvention<Translated>
{
    public void Apply(FluentNHibernate.Conventions.Instances.IPropertyInstance instance)
    {
        instance.Not.Update();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接